Thanks for clarification regarding Nim! (but, as I know, "reflection" is a
conception relating only for runtime. everything else is something different)
Nim is statically typed so only compile-time reflection (see module typetraits
for instance) is possible. Runtime reflection like in java is not possible (all
metadata stripped away by the backend compiler)
But is it possible in Nim to call procedure with parameters who name and
parameters will be known at runtime (and get result value)? Does Nim support
reflection?
Oh yes of course, you can store the procs in a table. Though you might have
issue with type-checking if they don't have the exact same arguments.
@timothee, use `procvar` annotation if you have the proc defined
proc echoa() {.procvar.} =
echo "proca"
proc echob() {.procvar.} =
echo "procb"
let echos = @[echoa, echob]
for f in echos:
f()
You can use together with enum as matchin
Here are 2 ways
## Sol 1 - if you can get by with identifier input
proc fun1() = echo "ok1"
proc fun2() = echo "ok2"
template callfun(fun: untyped) =
fun()
## Sol 2 - if you really need string input
## This also unrolls the loop
import macr
what would be the idiomatic way to write this D code in nim?
it calls a function by name (provided at runtime), trying all possibilities
until a match is found.
import std.stdio:writeln;
void fun1(){writeln("ok1");}
void fun2(){writeln("ok2");}
void callFun(string f