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

superclass mismatch in overloading of a generic open class #3423

Closed
ray-delossantos opened this issue Oct 15, 2016 · 9 comments
Closed

superclass mismatch in overloading of a generic open class #3423

ray-delossantos opened this issue Oct 15, 2016 · 9 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Milestone

Comments

@ray-delossantos
Copy link

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)


@ray-delossantos
Copy link
Author

Thanks to cjgajard for letting me know of the existence of this related issue: #3298.

@asterite
Copy link
Member

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

@cjgajard
Copy link
Contributor

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

@asterite asterite reopened this Oct 15, 2016
@asterite
Copy link
Member

Reduced:

class Foo(T)
end

class Bar(T) < Foo(T)
end

class Bar(T) < Foo(T)
end

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Oct 15, 2016
@asterite asterite added this to the 0.20.0 milestone Oct 15, 2016
@ray-delossantos
Copy link
Author

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

@ray-delossantos
Copy link
Author

@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 :).

@ray-delossantos
Copy link
Author

@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.

@asterite
Copy link
Member

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

@ray-delossantos
Copy link
Author

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

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

3 participants