module Test
class Foo; end
class Bar; end
TYPES = {
Foo => [] of Symbol,
Bar => [] of Symbol
}
end
https://play.crystal-lang.org/#/r/5avh
Syntax error in eval:6: unexpected ',' in type (use parentheses to disambiguate)
Only happens when having the comma on an empty array of T
You have to do what the error message tells you :-)
module Test
class Foo; end
class Bar; end
TYPES = {
Foo => ([] of Symbol),
Bar => ([] of Symbol)
}
end
The reason of the error is that Symbol, Bar could be the start of a Proc type.
Or use the classic syntax to create a new class instance:
module Test
class Foo; end
class Bar; end
TYPES = {
Foo => Array(Symbol).new,
Bar => Array(Symbol).new
}
end
@asterite @j8r Well now the new problem is i get:
undefined macro method 'Call#map'
CrSerializer::Assertions::NotNil => Array(Symbol).new,
and
undefined macro method 'Expressions#map'
CrSerializer::Assertions::NotNil => ([] of Symbol),
Since im iterating over the key/values (extra code removed for simplicity), and wanting to map the values in the array. tl;dr of this is im using it to new up classes that correspond with the name of the annotation.
{% for ivar in @type.instance_vars %}
{% for t, v in CrSerializer::Assertions::TYPES %}
{% ann = ivar.annotation(t.resolve) %}
{% if ann %}
{{v.map { |f| ann[f] }.join(',').id}}
{% end %}
{% end %}
{% end %}
Ideally this would be a lot cleaner if you could iterate over all the annotations on an ivar, and iterate on keys of the annotation, but that would prob be best for another issue.
For now i guess i'll just add a dummy var to the array and just do nothing with it in the initializer.
For future reference, solution was to do {% v = v.expressions[0] if v.is_a?(Expressions) %} to get the actual value of the expression if it was one.
Most helpful comment
Or use the classic syntax to create a new class instance: