contracts.ruby icon indicating copy to clipboard operation
contracts.ruby copied to clipboard

Overriding a function from an included module with contracts leads to errors

Open indigoviolet opened this issue 8 years ago • 3 comments

ContractError:
It looks like you are trying to use pattern-matching, but
multiple definitions for function '<function name here>' have the same
contract for input parameters:

None => String
None => String

if I do something like:

module Foo
   Contract None => String
   def foo

...

class Bar
   include Foo
    
   def foo

indigoviolet avatar Nov 24 '16 00:11 indigoviolet

In addition, that error message is not very useful, since it just repeats the (repeated) contract - would be more useful to show the offending code.

indigoviolet avatar Nov 24 '16 00:11 indigoviolet

I can't reproduce this, is there something I'm missing?

require "contracts"
include Contracts

module Foo
   Contract None => String
   def foo
    "hi"
   end
end

class Bar
  include Foo

  Contract None => String
  def foo
    "adit"
  end
end

p Bar.new.foo

egonSchiele avatar Jan 17 '17 21:01 egonSchiele

@egonSchiele I am currently working on a Rails project where the same ContractError will appear, but only when running RSpec tests via Spring preloader. So far I have not been able to track down the cause. I just need to comment out a few contracts to get RSpec to run. As the project is proprietary, I can't post the code here and so far I have not worked out a minimal example, but if you have any idea what role Spring is playing here, then maybe we could find one faster.

leoarnold avatar Mar 28 '21 15:03 leoarnold