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

Override only Contracts #219

Open
zerongjiang opened this issue Jan 15, 2016 · 2 comments
Open

Override only Contracts #219

zerongjiang opened this issue Jan 15, 2016 · 2 comments

Comments

@zerongjiang
Copy link

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
Copy link
Collaborator

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
Copy link
Collaborator

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

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