Closed fvirdia closed 2 months ago
I think this is scoped to NamedTuple.new
itself. I don't think I've ever seen someone using the constructor over the literal, which works just fine: {"a-b": ""}
. We should still fix it of course, but I'd be curious to know what the use case for even having NamedTuple.new
is over just using the literal...
Playing a bit with the "try it online" thingy, it seems this was working as of 1.4.1, and stopped working with 1.5.0.
I'd be curious to know what the use case for even having
NamedTuple.new
is over just using the literal...
inexperience with the language
Prob caused by https://github.com/crystal-lang/crystal/pull/12011.
I'd be curious to know what the use case for even having
NamedTuple.new
is over just using the literal...inexperience with the language
No worries, more so meant from a language perspective. I submitted a PR to fix this, but for now can just use the literal syntax if you're needing one with a hyphen.
That was quick!
I went back and looked at why I used the constructor, I think I was unsure whether I would be getting Hash(String, String)
. I wanted to make sure I knew the type I was instantiating. Again, at the time of stumbling across the bug (actually a week ago, it was on my todo list) I had been using crystal for about a day.
Is the literal guaranteed to always result in a NamedTuple?
Is the literal guaranteed to always result in a NamedTuple?
Yes, an "object" using {}
with :
between key and value is a NamedTuple. E.g. {foo: "bar"}
. However if you use =>
as the separator, you get a hash. E.g. {"foo" => "bar"}
.
Thanks!
Bug Report
Trying to use a string literal as NamedTuple key fails if the literal contains a dash
-
.Compiler info:
Example code
Compiler output: