Crystal: can't use A(T) as generic type argument yet, use a more specific type

Created on 1 Sep 2017  路  5Comments  路  Source: crystal-lang/crystal

Sorry for my "silly-from-side" questions, but

https://play.crystal-lang.org/#/r/2neg

abstract class A(T)
  def foo
    puts T.to_s
  end
end

class B < A(Int32)
end

a = [] of A
a << B.new
a.each { |e| p e.foo }

# => Error in line 10: can't use A(T) as generic type argument yet, use a more specific type

It's weird, because A is abstract. Any workarounds?

Most helpful comment

All 5 comments

You are not defining generic type on line 10:

abstract class A(T)
  def foo
    puts T.to_s
  end
end

class B < A(Int32)
end

a = [] of A(Int32) # <<<
a << B.new
a.each { |e| p e.foo }

https://carc.in/#/r/2nf6

@hugoabonizio the whole point of Generics is that there might be class C < A(String) or < A(AnotherType). There might be any amount of other classes.

@vladfaust But in [] of A, T is not specified. It could be anything. And that's not supported by the compiler... basically T would be Object here, but that's not supported...

@asterite will supporting Object be implemented some day?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nabeelomer picture nabeelomer  路  3Comments

asterite picture asterite  路  3Comments

will picture will  路  3Comments

oprypin picture oprypin  路  3Comments

grosser picture grosser  路  3Comments