-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
When expansion is too eager #2002
Comments
Now crashes the compiler:
|
New kind of crash:
|
Now it compiles again, printing |
I just wanted to see if there is a workaround here, but aparently if I write the proc with just a different syntax it doesn't even compile anymore: import typetraits
proc isNillable[T](t: typedesc[T]): bool {.compileTime.} =
when compiles((let v: T = nil)):
echo(T.name & " is nillable")
return true
else:
echo(T.name & " is not nillable")
return false
type
Foo[T] = object
when isNillable(T):
nillable: float
else:
notnillable: int
var val1: Foo[ref int]
var val2: Foo[int]
echo val1, val2 output:
This might be a serious overload resolution bug. But anyway, there is a workaround. I would try if a union would work here. This of course isn't checked, neither at compile time nor at runtime, but it enables you what you want to do. import typetraits
proc isNilable[T](t: typedesc[T]): bool {.compileTime.} =
when compiles((let v: T = nil)):
echo(T.name & " is nilable")
return true
else:
echo(T.name & " is not nilable")
return false
type
Foo[T] = object {.union.}
nilable: float
notnilable: int
proc `$`[T](arg: Foo[T]): string =
when isNilable(T):
return "(nilable: " & $arg.nilable & ")"
else:
return "(notnilable: " & $arg.nilable & ")"
var val1: Foo[ref int]
var val2: Foo[int]
echo val1, val2 output:
|
In 2.0: import typetraits
proc isNillable(T: typedesc): bool =
when compiles((let v: T = nil)):
echo(T.name & " is nillable")
return true
else:
echo(T.name & " is not nillable")
return false
type
Foo[T] = object
when isNillable(T):
nillable: float
else:
notnillable: int
var val1: Foo[ref int]
var val2: Foo[int]
echo val1, val2
|
closes nim-lang#12582, closes nim-lang#19552, closes nim-lang#2465, closes nim-lang#4596, closes nim-lang#15246, closes nim-lang#12683, closes nim-lang#7889, closes nim-lang#4547, closes nim-lang#12415, closes nim-lang#2002, closes nim-lang#1771, closes nim-lang#5121 The test for nim-lang#5648 is also moved into its own test from `types/tissues_types` due to not being joinable.
* test case haul for old generic/template/macro issues closes #12582, closes #19552, closes #2465, closes #4596, closes #15246, closes #12683, closes #7889, closes #4547, closes #12415, closes #2002, closes #1771, closes #5121 The test for #5648 is also moved into its own test from `types/tissues_types` due to not being joinable. * fix template gensym test (cherry picked from commit c19fd69)
* test case haul for old generic/template/macro issues closes #12582, closes #19552, closes #2465, closes #4596, closes #15246, closes #12683, closes #7889, closes #4547, closes #12415, closes #2002, closes #1771, closes #5121 The test for #5648 is also moved into its own test from `types/tissues_types` due to not being joinable. * fix template gensym test (cherry picked from commit c19fd69)
The text was updated successfully, but these errors were encountered: