-
-
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
Compiler problem in 0.16.0 with generics #2558
Comments
Yes :-( In general, generic classes inheritance is broken and I wouldn't recommend using it right now. This is not a limitation in the language, it's just that the current implementation for this isn't good enough (at the time I did it I overlooked some things). I have to sit down with @waj and @bcardiff and fix all issues around generic classes inheritance. Since we now have the new "global" type inference implemented, solving this is a next priority. At least the way I see it, it's where most bugs come from. As a workaround, until we fix this, you can include a dummy module in module OrdModule
end
abstract class Ord(A)
include OrdModule
abstract def cmp(a : A, b : A)
end
class IntOrd < Ord(Int32)
def cmp(a, b)
a > b
end
end
class Sorter(A)
@order : OrdModule
getter :order
def initialize(@order : Ord(A))
end
def sort(list : Array(A))
i = 0
j = list.size - 1
aux = nil
while i < j
if order.cmp(list[i], list[j])
aux = list[i]
list[i] = list[j]
list[j] = aux
end
i += 1
j -= 1
end
list
end
end
ls = [5, 5, 2]
puts Sorter(Int32).new(IntOrd.new).sort(ls)
puts ls |
How does this affect built-in generics? For example @porras' mock library has the following class which is no longer valid Crystal code in 0.16.0: module Mock
class Arguments
def initialize(arguments)
@arguments = arguments.to_a
end
def_equals @arguments
def to_s
@arguments.inspect
end
def self.empty
Empty.new
end
class Empty < Arguments
def initialize
end
def ==(other)
false
end
def ==(other : Empty)
true
end
end
end
end It complains first that the Arguments::Empty subclass doesn't set the |
Yes, in the future you will be able to have a variable of type Also note that it doesn't make much sense to have |
Okay, what about the case like class MethodStub
getter :value
# ...
def and_return(@value)
self
end
end where |
Closed in favor of #2665 |
This code isn't working in the 0.16.0 version
I got this
The text was updated successfully, but these errors were encountered: