-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compilation error when using generic inheritance, class variable and union #15120
Comments
You could replace |
Thanks @HertzDevil, this workaround (variant 3 below) does help me! The variant 2 below, however, also triggers the error. class X(T) # need to be generic
class_property x = 0 # need to be class variable
def initialize(@x : T|X(T)) # need to be union type
end
def m
# @@x # variant #1: triggers: Error: can't infer the type of class variable '@@x' of X(Int32)
# self.class.x # variant #2: triggers: Error: can't infer the type of class variable '@@x' of X(Int32).class
X(T).x # variant #3: works
x = @x
if x.is_a?(X(T))
x.m # need to call method
end
end
end
class Y(T) < X(T) # needed (but unused...)
end
x = X(Int32).new(42).m # triggers error in line 6 (see above) |
@wolfgang371 Avoid using |
When including this fix in my original program I run in the next problem (dunno if I should file an extra issue since they're just piling up anyhow) - stripped down version here: class Grid(T)
@input : Array(T|self)
def initialize(input = Array(T|self).new)
@input = input.map(&.as(T|self)) # cast to uniform type
end
def enumerate_grids : Array(Tuple(Int32, self))
grids = [{0, self}]
@input.each do |cell|
if cell.is_a?(self)
grids += cell.enumerate_grids.map do |level, grid|
{level+1, grid}
end
end
end
grids
end
end
class GridX(T) < Grid(T) # no error if commented out...
end
Grid(Int32).new([10,20]).enumerate_grids
# triggers (newlines inserted manually):
# Error: method Grid(Int32)#enumerate_grids must return
#
# Array(Tuple(Int32, Grid(Int32)))
#
# but it is returning (
#
# Array(Tuple(Int32, Grid(Int32))) |
# Array(Tuple(Int32, Grid(Int32)))
#
# ) |
Funny enough, the latter bug already seems to be rather resistant. With the usual antidote the compile time error mutates to a runtime error: # ...
grids.as(Array(Tuple(Int32, Grid(Int32)))) # instead of just returning plain `grids`
# ... yields (whitespace added manually) Unhandled exception: Cast from
Array(Tuple(Int32, Grid(Int32)))
to
Array(Tuple(Int32, Grid(Int32)))
failed, at /tmp/inheritance-bug.cr:15:9:15 (TypeCastError) 😉 Seems like I need to copy and paste instead of inheriting here... 😕 |
This is the stripped-down example:
I don't have a workaround for this... 😦
I'm running
The text was updated successfully, but these errors were encountered: