Right now inheriting from generic types, and even inheriting a non-generic type that's not Reference
is broken. There are many issues related to this, which I'll link here:
inherited
work with generics And one pending decision:
All of these should be fixed. The main issue is that the current implementation is very weak and needs a redesign. This is only a matter of time.
This issue will be used to mark all bugs related to generics as a duplicate of this one, because it's essentially one problem.
I'll keep this issue open for now, until we test it a bit more, but all or most of the bugs above are now fixed on master.
I'm not really sure about this example, should this work or not?
@benoist what seems to be the problem in that example?
Ah sorry I should have explained it a bit better. I'd expected that adding 2 Int32Arrays it would also return an Int32Array. This is currently not the case
Well, Array#+
is documented to return an Array, and not an instance of the same class that received the +
message. As such, I'd not expect Int32Array
to be returned in that example.
Anyhow, if you do believe it should, or have a similar issue, please open a new issue here; the idea of this one is to act as an "index" of generic-related stuff.
No I think I agree with you. If I want it to return a concatenation of self I should explicitly do so.
Thinking about it, inheriting from a generic is probably not a good idea anyway, composition is probably better.
That's what I was going to suggest: either composition, or just declaring an alias if what you want is to have a friendlier name for a particular instantiation of a generic.
Would further bug reports regarding generic inheritance be useful, or just noise at this point?
@ezrast Bug reports are always welcome
Could we strike through in description issues/prs which were closed/merged? /cc @asterite
This issue should be closed and made a project or label.
This came up today.
It appears that all related issues pointed out here have been closed and released, so I'm recommending closing this issue.
@miketheman, The kernel of the issue, that "the current implementation is very weak and needs a redesign", has not yet been addressed to my knowledge. Several related bugs remain in latest Crystal, such as #4059 (which was closed as a duplicate of this issue), #5290, and this:
module Base(T); end
module Derived; extend Base(Int32); end
arr = [Derived] of Base(Int32)
pp arr.first == arr.first # false
pp Derived == arr.first # false
pp arr.first == Derived # true
@ezrast please open as a seperate issue, that code shouldn't compile as Derived
is Derived.class
and Base(Int32).class
. Derived.new
wouldn't compile either, if it was a class and had a ctor that code would compile fine.
Considering there's a new issue to track the Derived
status, I think this issue could be closed.
@miketheman As far as I can see lot of issues have been closed (without resolution) in favor of this one, so it's kinda recursive situation. Closing this will leave 'em forgotten for the future.
Most helpful comment
I'll keep this issue open for now, until we test it a bit more, but all or most of the bugs above are now fixed on master.