Thanks @mratsim for pointing me to those issues! I did not find them before. It 
looks like this isn't supposed to work and should be flagged by the compiler.

@shirleyquirk thank you for also looking into the issue. I had tried that but 
with my object init proc it only works if the procedure is also called at 
runtime. You can test this by using:
    
    
    proc initNode: Node =
      echo "init node"
      result.kind = A
      result.children = @[Node(), Node()]
    
    
    Run

and changing the const decl to
    
    
    let root{.compiletime.} = initNode()
    
    
    Run

When run you will see that initNode is called at compiletime and also at 
runtime. This doesn't work for my usecase where the information is only 
available at compiletime. If the proc is marked with the {.compileTime.} pragma 
it is not called at runtime and the fields are zeroed out again.

Just for completeness here are the things I tried before creating this forum 
post:
    
    
    const root = initNode()
    # doesn't work as discussed above, some fields are zeroed out at runtime
    
    
    Run
    
    
    let root {.compileTime.} = initNode()
    # doesn't work as explained above, initNode must be called also at runtime 
to work correctly
    
    
    Run
    
    
    let root = static(initNode())
    # doesn't work, same as example 1
    
    
    Run
    
    
    var root {.compileTime.}: Node
    static: root = initNode()
    # stops initNode being called at runtime without the {.compileTime.} pragma 
on the proc, but fields are still zeroed out
    
    
    Run

Reply via email to