The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
A macro generates code only in the location in which it is executed.
However.....you can make what you want to do work with macros.
Macros have access to constants.
So, if you generate all of your procs, assigned to a constant name, under a common namespace, then in your main.cr you can iterate over
that namespace (see Crystal::Maros::TypeNode in the Crystal API docs), find the names of all of your Proc constants, and build your master hash.
#register
method then too
macro some_name
are reusable portions of macro code, but you can also use the macro code outside of them
class Foo
ONE = 1
TWO = 2
end
{{pp Foo.constants}} # => [One, Two]
@guest64 --
module MyProcs
Proc1 = ->(arg0 : Int32, arg1 : Int32) { arg0 + arg1 }
end
module MyProcs
Proc2 = ->(arg0 : String, arg1 : String) { arg0.to_i64 + arg1.to_i64 }
end
macro build_a_hash
{%
foo = {} of Nil => Nil
MyProcs.constants.each do |const|
foo[const.stringify] = "MyProcs::#{const.id}".id
end
%}
MyHash = {{ foo.stringify.id }}
end
build_a_hash
pp MyHash
pp typeof(MyHash)
You are still going to have problems with types, though, if you have a single hash that holds everything.
Try it and you will see what I mean.
@[Measure]
def foo
end
macro finished
{% for meth in ... %}
def {{meth...}}
slot = {{ slot_for @type, meth.name }}
Fiber.current.measurements[slot].measure do
previous_def
end
end
{% end %}
end