Re:Pattern Matching for Java
by mark242 on Wednesday April 19, 2017 @10:31PM
(#14153498)
In reality my function would rarely exist in the Scala world since having a weakly-typed return like that is cringeworthy to say the least. But it's better to see this in action with Scala options.
[ Parent
| on HN ]
With Java 8 having Optional and Lambdas, this is no longer the case:
final Optional<String> myOptionalVar = functionThatReturnsOptional();
final String output = myOptionalVar
.map(str -> str.length() > 10
? "This is a long string."
: "This is a short string.")
.orElse("This code would be fifteen lines of null checking in Java.");
System.out.println(output);
Re:Pattern Matching for Java
by tylerpachal on Thursday April 20, 2017 @12:48PM
(#14158361)
I think this is less-rare than you think. In my Scala code, I have a lot of matching on weakly-typed values which come from parsing JSON objects (where I also deal with a lot of optional values).