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 mismatch error when use generic as typedesc in templates #20033

Closed
gabbhack opened this issue Jul 15, 2022 · 1 comment · Fixed by #22535
Closed

type mismatch error when use generic as typedesc in templates #20033

gabbhack opened this issue Jul 15, 2022 · 1 comment · Fixed by #22535

Comments

@gabbhack
Copy link

gabbhack commented Jul 15, 2022

Example

template run[T](): T = default(T)

echo run[int]()

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

Current Output

Error: type mismatch: got <int>
but expected one of:
proc default[T](_: typedesc[T]): T
  first type mismatch at position: 1
  required type for _: typedesc[T]
  but expression 'T' is of type: int

expression: default(T)

Expected Output

0

This work if use proc instead of template

Workaround

template run[T](): T = default(typedesc[T])

echo run[int]()

Maybe #2005 is related.

$ 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
@gabbhack gabbhack changed the title Cant use generic as typedesc in templates type mismatch error when use generic as typedesc in templates Jul 15, 2022
@metagn
Copy link
Collaborator

metagn commented Jul 15, 2022

Related: #6340 and others

bung87 added a commit to bung87/Nim that referenced this issue Sep 20, 2022
metagn added a commit to metagn/Nim that referenced this issue Aug 23, 2023
jangko pushed a commit to jangko/nimPNG that referenced this issue Aug 23, 2023
Below `newStorage[T]` and `newStorageOfCap[T]` call `getUnderlyingType(T)`, which shouldn't work because `T` is a type and `getUnderlyingType` takes an `openArray` argument. However it works due to a [Nim bug](nim-lang/Nim#20033) where `T` is treated as a *value* of its type.

To make this work with and without the bug fixed, add an additional `getUnderlyingType` overload that takes a `type openArray` parameter instead.
metagn added a commit to metagn/Nim that referenced this issue Aug 25, 2023
Araq pushed a commit that referenced this issue Aug 25, 2023
* fix generic param substitution in templates

fixes #13527, fixes #17240, fixes #6340, fixes #20033, fixes #19576, fixes #19076

* fix bare except in test, test updated packages in CI
narimiran pushed a commit that referenced this issue Dec 1, 2023
* fix generic param substitution in templates

fixes #13527, fixes #17240, fixes #6340, fixes #20033, fixes #19576, fixes #19076

* fix bare except in test, test updated packages in CI

(cherry picked from commit 1cc4d3f)
narimiran pushed a commit that referenced this issue Dec 1, 2023
* fix generic param substitution in templates

fixes #13527, fixes #17240, fixes #6340, fixes #20033, fixes #19576, fixes #19076

* fix bare except in test, test updated packages in CI

(cherry picked from commit 1cc4d3f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants