1

Topic: Optional (in Java)

Whether helps in something? I thought that in case of call Optional:: get () c by the missing link (null) is thrown out  an exception that forces to turn each call get () in try/catch that in turn guarantees that any reversal to object will check a case of absence of the link. But there RuntimeException, therefore hinders nothing causing get () to forget to make check and instead of NullPointerException to receive NoSuchElementException. Unique advantage which I found, is shorter syntax in a case when it is necessary to replace the zero link with value on-default. In this case something is written like Optional.ofNullable (maybeNullable).orElse (nonNullable).

2

Re: Optional (in Java)

M> something In this case is written like Optional.ofNullable (maybeNullable).orElse (nonNullable). And still between ofNullable and orElse much that it is possible to interpose

3

Re: Optional (in Java)

Hello, mbait, you wrote: whether M> Helps in something? I thought that in case of call Optional:: get () c by the missing link (null) is thrown out  an exception that forces to turn each call get () in try/catch that in turn guarantees that any reversal to object will check a case of absence of the link. But there RuntimeException, therefore hinders nothing causing get () to forget to make check and instead of NullPointerException to receive NoSuchElementException. M> Unique advantage which I found, is shorter syntax in a case when it is necessary to replace the zero link with value on-default. In this case something is written like Optional.ofNullable (maybeNullable).orElse (nonNullable). Well for example the fact of the declaration of variable type / returned value as Optional <T>, it is explicit at type level speak about that that values can not be. interface UserDao <Id> {//all obviously the user does not wait here NoSuchUserException extends RuntimeException Optional <User> findById (Id id)//and here here it the devil only knows returns null or throws exception User get (Id id)} Well and everyone there in  handling clearly, safe conversions, checks of conditions. Optional <User> user = userDao.findById (...); return user.stream ().filter (u->! u.isBlocked ()).map (ActiveUserDTO:: new).elseThrow (()-> new UserBlockedException ()); In java9 like as Optional implements Iterable that allows to use for example Optional <User> maybeUser = userDao.findById (...); for (User u: maybeUser) {//here User precisely is}//instead of traditional if (maybeUser.isPresent ()) {//process User} else {//doRegret} But that such, for fans of the strange

4

Re: Optional (in Java)

In ... That it is not necessary to do with Optional, it is not necessary to accept it on an input interface UserValidator {boolean isValid (Optional <User> maybeUer)} It is the bad idea, thereby we lose possibility to use UserValidator for stream handling of arbitrary

5

Re: Optional (in Java)

Well and in a combination in summaries for example JSR-305 it is quite possible to speak about any null-safety.

6

Re: Optional (in Java)

Hello, frm, you wrote:> In java9 like as Optional implements Iterable Check of this fact would occupy it of less time, than writing of the subsequent text. https://docs.oracle.com/javase/9/docs/a … ional.html the right answer: https://docs.oracle.com/javase/9/docs/a … tml#stream -

7

Re: Optional (in Java)

Hello, mbait, you wrote: M> therefore hinders nothing causing get () to forget to make check and instead of NullPointerException to receive NoSuchElementException. Intellij IDEA, knowing about semantics Optional, catches such situations and highlights problem places. Well and NoSuchElement it is already more interesting and, often to investigate easier, than NPE.