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

type expected error when using type parameter with in template (works with proc) #11941

Closed
arnetheduck opened this issue Aug 14, 2019 · 4 comments
Labels
Milestone

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Aug 14, 2019

type X = object
  e: int

func works(T: type X, v: auto): T = T(e: v)
template fails(T: type X, v: auto): T = T(e: v)

var 
  w = X.works(42)
  x = X.fails(42)
[arnetheduck@tempus tmp]$ nim c -r testvlang.io
Hint: used config file '/home/arnetheduck/.choosenim/toolchains/nim-0.20.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: test [Processing]
/home/arnetheduck/tmp/test.nim(9, 8) Error: type expected
[arnetheduck@tempus tmp]$ nim --version
Nim Compiler Version 0.20.2 [Linux: amd64]
Compiled at 2019-07-17
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 88a0edba4b1a3d535b54336fd589746add54e937
active boot switches: -d:release
@zah
Copy link
Member

zah commented Aug 14, 2019

There are some issues in the resolution of generic parameters within templates. The following work-around should be fine for now:

func works(T: type X, v: auto): T = T(e: v)
template fails(T: type X, v: auto): auto = T(e: v)

@Araq Araq added the Severe label Aug 17, 2019
@krux02
Copy link
Contributor

krux02 commented Aug 17, 2019

This works as well:

func works[T](t: typedesc[T], v: auto): T = T(e: v)
template fails[T](t: typedesc[T], v: auto): T = T(e: v)

Generally I made good experience to always write out the generic parameter (T) for typedesc parameters and then use this generic parameter instead of the the typedesc value t.

@krux02
Copy link
Contributor

krux02 commented Aug 18, 2019

This PR here is related even though it doesn't fix it yet: #11959

@arnetheduck
Copy link
Contributor Author

arnetheduck commented Aug 20, 2019

my end goal btw is that this works with generics too (I don't feel that it's terribly valuable to add E to fails explicitly in this case - in particular, I want to forward v to the object init ideally without converting it to E eagerly.

type X[E] = object
  e: E

template fails(T: type X, v: auto): T = T(e: v)

@Araq Araq added this to the v1 milestone Aug 23, 2019
Araq added a commit that referenced this issue Aug 28, 2019
@Araq Araq closed this as completed in 5f7a6af Aug 28, 2019
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

4 participants