You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No type error when deleting from an array an invalid type #988
# Mark a register as no longer used.deffreereg(reg)
@usedregs.delete reg
end# Free a register if it's a register item.defofree(maybereg)
self.freereg maybereg.reg if maybereg.is_a? Parser::RegItemend
I had originally written the second-to-last line as:
self.freereg maybereg if maybereg.is_a? Parser::RegItem
@usedregs is supposed to be of the type Register, which is completely independent from Parser::RegItem. Because I was passing the wrong type to freereg, the register was never freed. This caused that to lurk as a bug until my compiler crashed because it ran out of registers.
In Java for example removing an element is not restricted to a type. This is because you delete elements by equality. So, this works in Ruby (and in Crystal):
a = [1, 2, 3]
a.delete 1.0
a #=> [2, 3]
So I'm not sure what we should do here. This is an excellent opportunity to discuss it :-)
@asterite Well, Ruby is duck-typed, and Java is pretty stupid. ;)
I'm just saying this because I don't think anyone has ever done that intentionally, as it doesn't really serve a purpose. What would the point of it be? a could never hold floats, anyway.
This (and hash-key types) could be added as checks for a linter if it's desired to not break stdlib. I'm currently working on adding the ability to have semantic rules like this to ameba.
Reacted by Johannes Müller, ven and Julien Portalier
Activity
refi64 commentedon Jul 16, 2015
Real-life example:
Consider these two functions:
I had originally written the second-to-last line as:
@usedregsis supposed to be of the typeRegister, which is completely independent fromParser::RegItem. Because I was passing the wrong type tofreereg, the register was never freed. This caused that to lurk as a bug until my compiler crashed because it ran out of registers.refi64 commentedon Jul 16, 2015
Possible fix: change line 418 of
array.crto:asterite commentedon Jul 16, 2015
This is interesting :-)
In Java for example removing an element is not restricted to a type. This is because you delete elements by equality. So, this works in Ruby (and in Crystal):
So I'm not sure what we should do here. This is an excellent opportunity to discuss it :-)
refi64 commentedon Jul 16, 2015
@asterite Well, Ruby is duck-typed, and Java is pretty stupid. ;)
I'm just saying this because I don't think anyone has ever done that intentionally, as it doesn't really serve a purpose. What would the point of it be?
acould never hold floats, anyway.Just my three cents. :)
vendethiel commentedon Jul 16, 2015
I think I side with @kirbyfan64 here, for the sole purpose of catching type errors earlier
asterite commentedon Jul 18, 2015
I think it makes sense to put a type restriction there. The same happens with
Hash#[], which doesn't have a restriction but maybe it should.rdp commentedon Jul 18, 2018
Another sighting:
https://stackoverflow.com/questions/51092535/hash-includes-gives-weird-result-in-crystal/51092582?noredirect=1#comment89758815_51092582
straight-shoota commentedon Dec 7, 2020
For hash there's #8893
nobodywasishere commentedon Jan 27, 2025
This (and hash-key types) could be added as checks for a linter if it's desired to not break stdlib. I'm currently working on adding the ability to have semantic rules like this to ameba.