On Tuesday, June 4, 2013 7:56:24 PM UTC+2, clay wrote:
What do you want in "exists" and "forAll" that can't be done with the current "isPresent" and "forEach"?
First of all, to achieve anything similar with isPresent/forEach (isPresent is now called forEach) you have to code with side effects (it takes a lambda with a void return type), where the lambda you pass in have to mutate a boolean variable or something similar. It will be more code, and more error prone.
Second, the exists and forAll has different meanings, depending on whether you have a some or a none, for example:
Some(1).forAll(i -> i%2 == 0) returns false
Some(1).exists(i -> i%2 == 0) return false
Some(2).forall(i -> i%2 == 0) returns true
Some(2).exists(i -> i%2 == 0) return true
None.exists(i -> i%2 == 0) returns false
None.forall(i -> i%2 == 0) returns true
It is nice to have when doing validation for example, where you allow a field to be unset, but if it is set, it has to follow a certain rule, or the opposite, where you require something to be be Some (in one context) and validate according to a method. Let's say you have a Customer object, where the e-mail is Optional, but if it is some, then you want it to be a valid e-mail (good use case for forall). The same Customer object might have a phone number, which is mandatory, but represented by a Optional, as your legacy customer db don't have telephone numbers for all customers (good use case for exists).