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

typedesc[T] can't be used at compile time, unlike generics #8486

Closed
timotheecour opened this issue Jul 31, 2018 · 8 comments
Closed

typedesc[T] can't be used at compile time, unlike generics #8486

timotheecour opened this issue Jul 31, 2018 · 8 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Jul 31, 2018

typedesc[T] can't be used at compile time, unlike generics, as shown below:

when defined(case_default_old):
  template default*[T](t: typedesc[T]): T =
    # Error: cannot evaluate at compile time: v116407 var v: T
    var v: T
    v
else:
  proc default*[T]():T=
    var a: T
    return a

proc len(T: typedesc[tuple]):auto=
  when defined(case_default_old):
    const t=default(T)
  else:
    const t=default[T]()

  var n=0
  for _ in t.fields:
    inc n
  return n

proc test()=
  var a=(1,2,3)
  const length = a.type.len
  echo length

test()

Can that limitation be lifted?

NOTE: this affect the debate here: [RFC] guidelines for when to use typedesc vs generics #7517
NOTE: the motivating example was from default should be public instead of redefined everywhere; should use default[T] instead of default(T) #8485 ; if this limitation were lifted, we'd be able to use default(T) (or int.default) at compile time, instead of having to resort to default[T]()

EDIT /cc @yglukhov NOTE: see workaround below via intermediate defaultImpl[T]():T

proc defaultImpl[T]():T=
  var a: T
  return a
template default*[T](t: typedesc[T]): T = defaultImpl[T]()
@yglukhov
Copy link
Member

Works with devel

template default*[T](t: typedesc[T]): T =
    var v: T
    v
const a = default(int)

@timotheecour
Copy link
Member Author

@yglukhov I know, I'm using devel; it works in your example, but not the example I mentioned above:

rnim -d:case_default_old bugs/t74_typedesc_CT.nim
nim c --nimcache:/tmp/nim//nimcache/ -o:/tmp/nim//app -r -d:case_default_old bugs/t74_typedesc_CT.nim
/Users/timothee/git_clone/nim/timn/bugs/t74_typedesc_CT.nim(26, 24) template/generic instantiation from here
/Users/timothee/git_clone/nim/timn/bugs/t74_typedesc_CT.nim(6, 9) Error: cannot evaluate at compile time: v116407
      var v: T
          ^


rnim bugs/t74_typedesc_CT.nim
nim c --nimcache:/tmp/nim//nimcache/ -o:/tmp/nim//app -r bugs/t74_typedesc_CT.nim
CC: timn_t74_typedesc_CT
Hint: operation successful (12210 lines compiled; 0.614 sec total; 16.25MiB peakmem; Debug Build) [SuccessX]
3

@yglukhov
Copy link
Member

My bad, sorry.

@dom96
Copy link
Contributor

dom96 commented Jul 31, 2018

Duplicate of #7596. (Please consider grouping related issues together).

@dom96 dom96 closed this as completed Jul 31, 2018
@timotheecour
Copy link
Member Author

@dom96 how about adding a github label duplicate? (I can't as I don't have the required priviledge in this repo); so this can be marked as closed and duplicate

@dom96
Copy link
Contributor

dom96 commented Aug 1, 2018

Why would it be useful to find duplicated issues?

@timotheecour
Copy link
Member Author

Why would it be useful to find duplicated issues?

=> moved discussion here #8507

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

4 participants