I like the use of require
, assert
, assume
, and ensuring to document pre-conditions and post-conditions of a given function, but I have also come to use scalaz.Validation
mostly to perform checks that require used to do. But, lately, I have been most interested in using PartialFunction
, possibly with Validation
, to document a pre-condition. Something like:
case class Summary(user: String, at: DateTime, ....)
val restricted_add: PartialFunction[(Summary, Summary), Summary] = {
case (s1: Summary, s2: Summary) if s1.user != s2.user && s1.at == s2.at =>
}
with a utility function for capturing and converting a match error into a validation failure (e.g. restricted_add.lift(_).toSucess("Cannot add.")
). Somehow the case clause above seems less noisy than a For
with scalaz.Validation
. But I feel like I am going against the language. I'm not using def
's natural syntax for defining parameters, every client of such a function would have to use a utility function when invoking it, I would be capturing exceptions and converting them into validations, instead of working only with validations or only with exceptions.
What seems like the least noisy, most functional way of documenting these pre-conditions through code? Validation
returning functions, require
statements, PartialFunction
, other...?