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

Merging nnkArgList and nnkBracket #8832

Open
mratsim opened this issue Sep 1, 2018 · 1 comment
Open

Merging nnkArgList and nnkBracket #8832

mratsim opened this issue Sep 1, 2018 · 1 comment
Labels

Comments

@mratsim
Copy link
Collaborator

mratsim commented Sep 1, 2018

This is a follow-up on the discussion here: #8715 (comment).

For me this is the main reason I use nnkArgList, example taken from #7737.

import macros, typetraits, sequtils

proc getSubType*(T: NimNode): NimNode =
  echo getTypeInst(T).treerepr # <---- Output comes from here
  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()
  var containers = nnkBracket.newTree()

  for arg in x:
    if eqIdent(arg[0], "in"):
      containers.add arg[2]

  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 getSubType proc returns:

# Case nnkArgList
BracketExpr
  Sym "seq"
  Sym "int"

and

# Case nnkBracket
BracketExpr
  Sym "array"
  Infix
    Ident ".."
    IntLit 0
    IntLit 2
  BracketExpr
    Sym "seq"
    Sym "int"

i.e. varargs[typed] needs nnkArgList for proper type resolution.
cc @Araq @zah @LemonBoy

Related to #7719/nim-lang/RFCs#44 (Working with types in macro is difficult)

@LemonBoy
Copy link
Contributor

LemonBoy commented Sep 1, 2018

I'd argue that we just need a bit of consistency here, let's use nkArgList for argument lists and nkBracket for everything else instead of this awkward situation where you can mix&match nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants