egonSchiele / contracts.ruby

Contracts for Ruby.
http://egonschiele.github.com/contracts.ruby
BSD 2-Clause "Simplified" License
1.44k stars 82 forks source link

Override only Contracts #219

Open zerongjiang opened 8 years ago

zerongjiang commented 8 years ago

Is that possible to make this work?

Only override the contract for the override method,

class Super
  include Contracts

  Contract Num => Any
  def initialize(arg)
    puts arg
  end
end

class Sub < Super
  Contract String => Any
  def initialize(arg)
    super
  end
end

Super.new(11) 
Sub.new(11)        #Contract Violation:  Expected: String
Sub.new('hello')  # Contract Violation:  Expected: Num

Thanks!

waterlink commented 8 years ago

If you examine the a rough callstack of Sub.new(11) call:

- `main`
- `Contract String => Any - validator`
- `Sub.initialize`
- `super`
- `Contract Num => Any - validator`
- `Super.initialize`

I don't think it is possible at the moment. For that to be possible. It looks like there should be a way to unregister the contract for the current class. Even then, I don't think it would be easy to implement. Not saying that it will be weird in the user code..

waterlink commented 8 years ago

Another thing is to try making some helper, like contracts_super for usage instead of super. But that will change your code too much (you will have to provide all arguments to contracts_super at any point of time). And it will be way confusing for any new to the codebase..