Skip to content

getTypeInst for generics does not gives the full instantiation info in certain cases #7737

Closed
@mratsim

Description

@mratsim

Another case of difficulties working with types #7719/nim-lang/RFCs#44 and subtypes #6454 in macros.

In the following case I am trying to extract a subtype of multiple containers. It works fine for the built-in seq but not for a generic type. The subtype is not available through getTypeInst.

import macros, typetraits, sequtils

type
  CustomSeq*[T] = object
    data*: seq[T]

proc getSubType*(T: NimNode): NimNode =
  echo getTypeInst(T).treerepr
  result = getTypeInst(T)[1]

macro typed_helper(x: varargs[typed]): untyped =
  let foo = getSubType(x[0])

  result = quote do: discard

macro untyped_heavylifting(x: varargs[untyped]): untyped =

  var containers = nnkArgList.newTree()

  for arg in x:
    case arg.kind:
    of nnkInfix:
      if eqIdent(arg[0], "in"):
        containers.add arg[2]
    else:
      discard

  result = quote do:
    typed_helper(`containers`)

var a, b, c: seq[int]

untyped_heavylifting z in c, x in a, y in b:
  discard

## The following gives me CustomSeq instead
## of CustomSeq[int] in getTypeInst
var u, v, w: CustomSeq[int]

untyped_heavylifting z in u, x in v, y in w:
  discard

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions