Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generating instance of generic type from a macro is very very limited #7974

Closed
krux02 opened this issue Jun 6, 2018 · 0 comments · Fixed by #22023
Closed

generating instance of generic type from a macro is very very limited #7974

krux02 opened this issue Jun 6, 2018 · 0 comments · Fixed by #22023
Assignees

Comments

@krux02
Copy link
Contributor

krux02 commented Jun 6, 2018

I try to generate the entire tree of nnkObjectTy for a generic type entirely in a macro. In some very very limited contexts this is already possible, but as soon as I generate an actiual node of nnkObjectTy or nnkTupleTy, the compiler complains and says that it is either illegal or it can't generate VM code. Here is the example to reproduce the problem:

import macros

macro genTypeA(arg: typed): untyped =
  if arg.typeKind != ntyTypeDesc:
    error("expected typedesc", arg)

  result = arg.getTypeInst[1]

macro genTypeB(arg: typed): untyped =
  if arg.typeKind != ntyTypeDesc:
    error("expected typedesc", arg)


  let typeSym = arg.getTypeInst[1]
  result =
    nnkTupleTy.newTree(
      nnkIdentDefs.newTree(
        ident"a", typeSym, newEmptyNode()
      )
    )

type
  # this is the trivial case, MyTypeA[T] is basically just T, nothing else. But it works.
  MyTypeA[T] = genTypeA(T)
  # in this case I generate `tuple[a: T]`. This this is something the compiler does not want
  MyTypeB[T] = genTypeB(T)

# these are just alias types for int32 and float32, nothing really happens, but it works
var a1: MyTypeA[int32]
var a2: MyTypeA[float32]
var a3: MyTypeA[float32]

var b1: MyTypeB[int32]   # cannot generate VM code fur tuple[a: int32]
var b2: MyTypeB[float32]
var b3: MyTypeB[float32]
@zah zah added the Generics label Jun 7, 2018
@zah zah self-assigned this Jun 7, 2018
metagn added a commit to metagn/Nim that referenced this issue Jun 6, 2023
Araq pushed a commit that referenced this issue Jun 6, 2023
close #7974 by adding test case
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants