Hello folks.
I was playing with nim around for a couple of weeks, but and now i struggle
with forward declaration and later use of the function pointer.
Here is minimal example:
proc get_descriptions():string;
type
Function = object
description: string
action: (proc(): string)
proc new_function(desc: string, act: proc():string = nil): Function=
result.description = desc
if act == nil:
result.action = () => desc
let functions={
"help" : new_function(desc = "help function", act =
get_descriptions),
"start" : new_function(desc = "start function" )
}.toTable
proc get_descriptions(): string =
result = ""
for k in functions.keys:
result = fmt"{result} {k}: " & functions[k].description & "\n"
echo functions["start"].action() # <-- executes normally
echo functions["help"].action() # <-- issues "SIGSEGV: Illegal storage
access. (Attempt to read from nil?)"
Run
What am i doing wrong? Is it desired behaviour? If it is, how can i get around?
Thanks in advance!