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

Strange Proc return type behavior #7473

Closed
Blacksmoke16 opened this issue Feb 22, 2019 · 2 comments
Closed

Strange Proc return type behavior #7473

Blacksmoke16 opened this issue Feb 22, 2019 · 2 comments

Comments

@Blacksmoke16
Copy link
Member

https://play.crystal-lang.org/#/r/6bxj

require "json"

class Base
end

module Foo
  def to_json(val : Int32)
    1
  end
end

class Model1 < Base
  include Foo  
end

class Model2 < Base
  include Foo  
end

proc = ->(key : String) do
  if key == "1"
    Model1.new
  else
    Model2.new
  end
end

r = proc.call "1"

pp r.to_json 8
Error in line 30: no overload matches 'Base#to_json' with type Int32
Overloads are:
 - Object#to_json(io : IO)
 - Object#to_json()

Few thing going on here.

  1. Making class Base abstract fixes the problem.
  2. Removing the require "json" changes the error, but should still work.
  3. Having the proc only return one type fixes the problem.
@asterite
Copy link
Member

Yes, this is expected behavior. Please read this.

@Blacksmoke16
Copy link
Member Author

Oh fair enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants