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/
AlwaysInline
annotation was implied, or a way to opt in/out
undefined macro method 'Path#instance_vars'
confused me way longer than i wanna admit... it refers to Crystal::Macros::Path
and not Path
:P
macro derive_from(owner, *names)
\{% for name in names %}
\{% var = owner.resolve.instance_vars.find{ |v| v == name } %}
property \{{name}} : \{{var.type}}
\{% end %}
def initialize( \{{ names.map{ |n| "@#{n}".id }.join("").id }} )
end
\{% debug %}
end
struct Nomad::Config
include SimpleConfig::Configuration
options_from Common::Config, nomad_datacenters, nomad_base_url, nomad_ssl_ca, nomad_ssl_key, nomad_ssl_cert
end
def initialize(@nomad_base_url : URI)
is pretty much the same and less confusing
module Common::Config
CONFIGS = {} of _ => _
macro config(type_decl)
{% CONFIGS[type_decl.var.id] = type_decl %}
getter {{type_decl}}
end
config some_value : String
config some_id : Int32
end
macro options_from(type, *configs)
def initialize(
{% for config in configs %}
{% config = Common::Config::CONFIGS[config.id] %}
@{{config}},
{% end %}
); end
{{debug}}
end
struct Some::Config
options_from Common::Config, some_id, some_value
end
Some::Config.new 10, "foo" # => Some::Config(@some_id=10, @some_value="foo")