-
Notifications
You must be signed in to change notification settings - Fork 82
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
Docs out of date for Contract.valid?
?
#250
Comments
Hi, require "contracts"
include Contracts
Contract.override_failure_callback do |data|
p data
end
Contract Num, Num => Num
def add a, b
a + b
end
add([], []) This will just print out the |
@egonSchiele thank you for the rapid response! I totally see how the callback is great for debugging, logging, and other side effects. But in my case I need the metadata back as a value, within the same context from which I called |
@aviflax |
Yeah, I could, but… I just realized, that in my specific case, I actually can use the callback mechanism, because in fact all I want to do with the failure metadata is log it… so I just need a side effect. So I think I can get that going. I guess I was just intuitively uncomfortable, initially, with the callback mechanism, because in general I don’t like callbacks; I like functions with inputs and outputs. If, for example, I needed to actually do something with the failure metadata, like collect multiple errors together and generate a report of some kind… I guess I could rig up a way to do so with callbacks, but it would just seem so much simpler to me to just get back a value from a simple function call. Would there be any interest in adding another method to Thanks! |
Sure, that sounds useful @aviflax . I'd say it would be medium difficulty, and would give you a nice tour of this codebase :) |
@aviflax This may be helpful: some_contract = Or[ ArrayOf[String], ArrayOf[Integer] ]
unless Contract.valid?(some_value, some_contract)
# logs "((a collection Array of String) or (a collection Array of Integer))"
logger.warn Contracts::Formatters::Expected.new( some_contract ).contract.to_s
end Generating the message is a bit verbose, but you can write your own helper method to make that easier. EDIT: Sorry, the above was more complex than it needed to be. You can simply do this logger.warn some_contract.to_s This only difference is that you won't have the outermost parentheses. |
Very helpful — thank you! |
The docs for
Contract.valid
read:but when I call it, I’m just getting back a boolean — not a tuple.
So:
Thanks!
The text was updated successfully, but these errors were encountered: