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

Generic arguments cannot be used in templates (raising undeclared identifier) #3498

Closed
nanoant opened this issue Oct 29, 2015 · 2 comments
Closed

Comments

@nanoant
Copy link
Member

nanoant commented Oct 29, 2015

Currently there is no way to use generic arguments in templates, as if their identifiers were not replaced by their generic argument values during template evaluation.

template defaultInt(t: int): expr = (var d: int; d)
template defaultFor[T](t: T): expr = (var d: T; d)

echo defaultInt(1) #<- expected 0
echo defaultFor(1) #<- expected 0, but got Error: undeclared identifier: 'T'
nanoant added a commit to nanoant/Nim that referenced this issue Oct 29, 2015
Previously it was not possible to use template arguments in template body as
the symbols were not resolved correctly leading to Error: undeclared
identifier: 'XX', eg.:

   template defaultOf[T](t: T): expr = (var d: T; d)
   echo defaultOf(1) #<- invalid identifier, but should output 0
nanoant added a commit to nanoant/Nim that referenced this issue Oct 29, 2015
Previously it was not possible to use template arguments in template body as
the symbols were not resolved correctly leading to Error: undeclared
identifier: 'XX', eg.:

   template defaultOf[T](t: T): expr = (var d: T; d)
   echo defaultOf(1) #<- invalid identifier, but should output 0
@ayum
Copy link

ayum commented Nov 3, 2015

I applied your patch to upstream devel and now compiler crashes on th following code:

type
  Tup1[T] = tuple
    t0: T
    t1: T
  Tup2[T] = tuple
    t0: T
    t2: T
  Tup[T] = Tup1[T]|Tup2[T]

template set[T](tup: Tup[T], e: expr, t: T) {.immediate.} =
  when type(tup) is Tup1[T]:
    echo "Tup1"
  else:
    echo "Tup2"
  tup.e = t

when isMainModule:
  var tup = (t0: 1, t2: 2)
  set(tup, t2, 1)

with error SIGSEGV: Illegal storage access. (Attempt to read from nil?). Is this construction somewhat malformed?

@Araq
Copy link
Member

Araq commented Nov 4, 2015

Yes, .immediate templates are not subject to type checking and about to become deprecated.

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

No branches or pull requests

3 participants