-
-
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
Defining abstract methods with generics fails #6762
Comments
Looks like a bug for me, because this code raises no error: class Foo(T)
def foo(x : T)
{% raise NotImplementedError %}
end
end
class Bar < Foo(String)
def foo(x : String)
puts x
end
end
Bar.new.foo("foo") |
Definitely a bug, workaround is just to loosen the type restriction. |
Got this bug again while creating an abstract rescue HTTP handler (https://carc.in/#/r/5tcy): abstract class Rescuer(T)
def call(context)
call_next(context)
rescue ex : T
handle(ex)
end
abstract def handle(ex : T)
end
class BasicRescuer < Rescuer(Exception)
def handle(ex : Exception)
puts ex.inspect_with_backtrace
end
end
BasicRescuer.new Error in line 8: abstract `def Rescuer(T)#handle(ex : T)` must be implemented by BasicRescuer I post it to prove that there are some real-world use-cases. |
Workaround: don't define the abstract method at all. |
As mentioned above in #6762 (comment), defining a non-abstract method with The example could be simplified, sorry about that require "http/server"
abstract class Rescuer(T)
include HTTP::Handler
def call(context)
call_next(context)
rescue error : T
handle(error)
end
def handle(error : T)
{% raise "Must be implemented" %}
end
end
class BasicRescuer < Rescuer(Exception)
def handle(error : Exception)
puts error.inspect_with_backtrace
end
end
server = HTTP::Server.new([BasicRescuer.new]) do |context|
context.response.print("Hello\n")
end
server.bind_tcp(5000)
puts "Listening"
server.listen in line 9: Must be implemented As @asterite mentioned in #7200, there might be a problem with module inclusion. It also seems to be related to #6701. |
Just to clarify, the abstract method check doesn't work well with generic inheritance. Please try to avoid inheriting from generic types right now. |
I have an issue with abstract methods and generics (note, java developer speaking here, so this might be just an misunderstanding).
When defining the following abstract class
I expected the implementation to be
This however gives a compile time error, where as using
def set(copy : Array)
works.The text was updated successfully, but these errors were encountered: