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

Can use param.T in return type only with type classes #20027

Closed
gabbhack opened this issue Jul 14, 2022 · 2 comments · Fixed by #24005
Closed

Can use param.T in return type only with type classes #20027

gabbhack opened this issue Jul 14, 2022 · 2 comments · Fixed by #24005

Comments

@gabbhack
Copy link

With type class

type Test[T] = object

proc run(self: Test): self.T = discard

discard run(Test[int]())

https://play.nim-lang.org/#ix=44m4

Current output

As expected

With generic

type Test[T] = object

proc run[T](self: Test[T]): self.T = discard

discard run(Test[int]())

https://play.nim-lang.org/#ix=44m5

Current Output

/usercode/in.nim(5, 12) template/generic instantiation of `run` from here
/usercode/in.nim(3, 29) Error: cannot evaluate at compile time: self

With auto

type Test[T] = object

proc run(self: Test[auto]): self.T = discard

discard run(Test[int]())

https://play.nim-lang.org/#ix=44m6

Current Output

/usercode/in.nim(3, 33) Error: undeclared field: 'T'
$ nim -v
Nim Compiler Version 1.6.6 [Windows: amd64]
Compiled at 2022-05-05
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release
@hamidb80
Copy link
Contributor

workaround:

type Test[T] = object

proc run[T](self: Test[T]): T = discard

discard run(Test[int]())

@metagn
Copy link
Collaborator

metagn commented Aug 13, 2022

Doing

type Test[T] = object

proc run[T](self: Test[T]): typeof(self).T = discard

discard run(Test[int]())

gives

fatal.nim(53)            sysFatal
Error: unhandled exception: field 'intVal' is not accessible for type 'TFullReg' using 'kind = rkNode' [FieldDefect]

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