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?)"
What am i doing wrong? Is it desired behaviour? If it is, how can i get around?
Thanks in advance!
doesn't compile for me.
Error: implementation of 'test.get_descriptions() [declared in test.nim(3, 6)]' expected
whats your nim version?
You never assigned result.act when act is not nil.
if act == nil:
result.action = () => desc
should be
if act == nil:
result.action = () => desc
else:
result.action = act