Crystal: superclass mismatch in overloading of a generic open class

Created on 15 Oct 2016  路  9Comments  路  Source: crystal-lang/crystal

I'm trying to overload an open class with generics. I'm receiving a superclass mismatch error, but i believe this code should be possible.

abstract class View(T)
  getter outbox : Hash(Symbol, T)

  def initialize(@outbox : Hash(Symbol, T))
  end

  abstract def render  
end

module Test
  class TestView(T) < View(T)
    def render
      pp outbox[:test]?.try &.as?(String).try &.upcase
    end
  end
end

module Test
  class TestView(T) < View(T)
    def render
      pp @outbox.class
    end
  end
end

test_view = Test::TestView.new({ 
  :test1 => 1 
})

test_view.render
Error in ./src/test.cr:148: superclass mismatch for class Test::TestView(T) (View(T) for View(T))

  class TestView(T) < View(T)
        ^

Mac OS X Sierra
Crystal 0.19.4 (2016-10-07)


bug compiler

All 9 comments

Thanks to cjgajard for letting me know of the existence of this related issue: https://github.com/crystal-lang/crystal/issues/3298.

@raydf I think it's a valid issue, why did you close it?

I'm sorry for making confusion, this is a bug by itself, but when you said: one should be able to overload/override by type, in gitter/irc, that was related to 3298

Reduced:

class Foo(T)
end

class Bar(T) < Foo(T)
end

class Bar(T) < Foo(T)
end

Hi @asterite :

I closed because i was able to override the original behavior with this:


module Test
  class TestView(T) < View(T)
    def render
      pp outbox[:test]?.try &.as?(String).try &.upcase
    end
  end
end

module Test
  class TestView(T)
    def render
      pp @outbox.class
    end
  end
end

But there's a need for generics specialization that would make sense in the long run :). Any other considerations on why you needed to change the compiler for taking in consideration the case?.

Thanks,

raydf

@asterite , sorry, now i see the other case when you want to override the inheritance part:

class TestView(T) < View2(T)
...
end

Nice to know that it was a quick fix :).

@asterite:

I compiled crystal>master and it doesn't allow to override the inherited parent:

superclass mismatch for class Test::TestView(T) (View2(T) for View(T))

  class TestView(T) < View2(T)
        ^

I believe this issue should be reopened.

@raydf You can't override a superclass, but specifying a same superclass is OK

Well, ok, then everything works right in the master branch.

Was this page helpful?
0 / 5 - 0 ratings