Scala Option.orElse in Java

The same idea as my previous question is a bit more advanced. I want to write the following Scala code in Java:

Option(System.getProperty("not.present")).orElse(Some("default")).get 

I tried this:

  final Function0<Option<String>> elseOption = new Function0<Option<String>>() { @Override public Option<String> apply() { return new Some<String>("default"); } }; final Option<String> notPresent = new Some<String>(System.getProperty("not.present")). orElse(elseOption).get(); 

But I get:

 <anonymous OptionDemo$1> is not abstract and does not override abstract method apply$mcD$sp() in scala.Function0 [error] orElse(new Function0<Option<String>>() { 
+4
source share
1 answer

Extend AbstractFunction0 instead of Function0 . This should give you the methods provided by the method.

+2
source

Source: https://habr.com/ru/post/1393694/


All Articles