Skip to content
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

Return type as generic base class is not allowed #2283

Closed
umurgdk opened this issue Mar 7, 2016 · 6 comments
Closed

Return type as generic base class is not allowed #2283

umurgdk opened this issue Mar 7, 2016 · 6 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler

Comments

@umurgdk
Copy link

umurgdk commented Mar 7, 2016

I have a method which returns an object typed ParserThen(Array(Something), Array(Something)) and its parent class is Parser(Array(Something)) but compiler complains about its type. I simplified the problem.

https://carc.in/#/r/tut

Error in line 30: instantiating 'test()'

in line 23: type must be A(Int32), not C(Char, Int32)
class A(T)
    def val
        [] of T
    end
end

class B(T) < A(T)
end

class C(T,U) < A(U)
    def initialize(@first : A(T), @second : A(U))
    end

    def first
        @first.val
    end

    def val
        @second.val
    end
end

def test : A(Int32)
    a = A(Char).new
    b = B(Int32).new

    C.new a, b
end

test
@asterite
Copy link
Member

asterite commented Mar 7, 2016

Hi,

Please provide the full code that triggers the error you are getting. I'm getting:

Error in ./foo.cr:1: undefined constant Parser

There's nothing we can do with that to help you.

@umurgdk
Copy link
Author

umurgdk commented Mar 7, 2016

@asterite I just simplified problem on the description and include a carc.in link to it.

@bcardiff
Copy link
Member

bcardiff commented Mar 7, 2016

The generics class model have some pitfalls. Inheriting from a generic class I think is the problematic thing currently. Composition instead of inheritance might offer a workaround.

https://carc.in/#/r/tv9
using C.new(a, b) as A(Int32)
leads to can't cast to A(Int32) because it was never instantiated (Exception)

https://carc.in/#/r/tvc
forcing a foo = A(Int32).new
leads to a Error: you've found a bug in the Crystal compiler. Bug: trying to upcast A(Int32) <- C(Char, Int32)

@bcardiff
Copy link
Member

bcardiff commented Mar 7, 2016

Might be related to #846

@asterite
Copy link
Member

asterite commented Mar 7, 2016

Yes, inheriting from a generic class will probably won't work in many cases, so for now I'd advice not to use that "feature". We'll probably fix that once type annotations of instance variables are mandatory. Chances of fixing this before that point are very low.

@jhass jhass added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Mar 7, 2016
@asterite
Copy link
Member

Closed in favor of #2665

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

No branches or pull requests

4 participants