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
Because types and constants are both named by Path nodes, the latter can be used in certain contexts that normally expect a type. In those cases the constant behaves as if it is the type of its initializer:
X=1
fn : ->X"".is_a?(X)
"".as(X)
sizeof(X)
# the above are equivalent to:
fn : ->Int32"".is_a?(Int32)
"".as(Int32)
sizeof(Int32)
# but the following are syntax errors:
fn : ->1"".is_a?(1)
"".as(1)
sizeof(1)
I think we should disallow such uses of constant Paths.
One problem is that the literal expander transforms while expressions into is_a?:
case""whenInt32whenXend# the above is equivalent to:
__temp =""if __temp.is_a?(Int32)
elsif __temp.is_a?(X)
end
Afterwards Crystal::MainVisitor transforms __temp.is_a?(X) into X === __temp, because the literal expander doesn't know which Paths are constants and which are types. So a workaround is needed if we want to phase out this usage of is_a?.
The text was updated successfully, but these errors were encountered:
Yeah, that was just a brain dump. I was previously thinking about the literal expander using a faux method to tell the main visitor that it's supposed to turn it into is_a for types and === for constants.
But I suppose we could just add a flag to IsA which signals that it was created as condition of a when branch and the main visitor should not error on a constant and instead transform to ===.
Because types and constants are both named by
Path
nodes, the latter can be used in certain contexts that normally expect a type. In those cases the constant behaves as if it is the type of its initializer:I think we should disallow such uses of constant
Path
s.One problem is that the literal expander transforms
while
expressions intois_a?
:Afterwards
Crystal::MainVisitor
transforms__temp.is_a?(X)
intoX === __temp
, because the literal expander doesn't know whichPath
s are constants and which are types. So a workaround is needed if we want to phase out this usage ofis_a?
.The text was updated successfully, but these errors were encountered: