You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The {.compileTime.} pragma throws Error: request to generate code for .compileTime proc: foo even in situations where Nim is able to generate a compile time result.
As a workaround we can use a static result.
Test case:
procfoo(x: SomeInteger): int {.compileTime.} =result= x.sizeof
procbar(x: SomeInteger): static[int] =result= x.sizeof
var a: int# echo foo(a) # {.compileTime.} failsechobar(a) # static[result] works
As far as I know, the {.compileTime.} pragma was supposed to be used with NimNode procs.
However in the latest releases, it is automatically done and it does not force compile-time function evaluation either.
So I propose that:
either we delete the pragma and document that compile-time function evaluation should use a static result.
or we keep it as an alias to static[ResultType] to fix the behaviour highlighted in the test case.
The text was updated successfully, but these errors were encountered:
The plan is that the compiler will implicitly figure out what can be evaluated at compile time. This will happen when a function doesn't have side effects and all of its parameters are known at compile-time.
I'm surprised that setting a static return type currently has the desired effect. I've never tried to implemented that. The supported way to force static evaluation is by wrapping your proc with a template like this:
of course you can't use a compile time proc in this case, because you can't pass a runtime value a to to a compile time function. The function bar works is in my opition just a bug, if you ever actually want that it is evaluates at compile time the whole thing will blow up in your face. The compiler should refuse to compile it as well. Either use a template as @zah suggested, or pass the typedesc like in this example:
var a: intprocfoo[T](t: typedesc[T]): int {.compileTime.} =result= t.sizeof
const x =foo(a.type)
The {.compileTime.} pragma throws
Error: request to generate code for .compileTime proc: foo
even in situations where Nim is able to generate a compile time result.As a workaround we can use a static result.
Test case:
As far as I know, the
{.compileTime.}
pragma was supposed to be used with NimNode procs.However in the latest releases, it is automatically done and it does not force compile-time function evaluation either.
So I propose that:
The text was updated successfully, but these errors were encountered: