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

Compiler internal error when using default value for a generic parameter. #16700

Closed
lscrd opened this issue Jan 12, 2021 · 1 comment · Fixed by #24065
Closed

Compiler internal error when using default value for a generic parameter. #16700

lscrd opened this issue Jan 12, 2021 · 1 comment · Fixed by #24065

Comments

@lscrd
Copy link

lscrd commented Jan 12, 2021

The following program causes an internal error in the compiler:

type MyObject[T] = object
  x: T

proc initMyObject[T](value = T.default): MyObject[T] =
  MyObject[T](x: value)

var obj = initMyObject[int]()

Current Output

Error: internal error: expr(skType); unknown symbol

Expected Output

Compilation successful.

Possible Solution

Explicitly specifying the type of the parameter solves the problem:

type MyObject[T] = object
  x: T

proc initMyObject[T](value: T = T.default): MyObject[T] =
  MyObject[T](x: value)

var obj = initMyObject[int]()

Additional Information

$ nim -v
Nim Compiler Version 1.4.2 [Linux: amd64]
Compiled at 2020-11-30
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 3fb5157ab1b666a5a5c34efde0f357a82d433d04
active boot switches: -d:release

Also present in development version.

@ringabout
Copy link
Member

Another workaround:

type MyObject[T] = object
  x: T

proc initMyObject[T](value = default(T)): MyObject[T] =
  MyObject[T](x: value)

var obj = initMyObject[int]()

metagn added a commit to metagn/Nim that referenced this issue Sep 6, 2024
@Araq Araq closed this as completed in a93c5d7 Sep 6, 2024
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

Successfully merging a pull request may close this issue.

2 participants