Why should Java 8's Optional not be used in arguments

前端 未结 20 1649
我寻月下人不归
我寻月下人不归 2020-11-22 11:33

I\'ve read on many Web sites Optional should be used as a return type only, and not used in method arguments. I\'m struggling to find a logical reason why. For example I h

20条回答
  •  太阳男子
    2020-11-22 12:29

    My take is that Optional should be a Monad and these are not conceivable in Java.

    In functional programming you deal with pure and higher order functions that take and compose their arguments only based on their "business domain type". Composing functions that feed on, or whose computation should be reported to, the real-world (so called side effects) requires the application of functions that take care of automatically unpacking the values out of the monads representing the outside world (State, Configuration, Futures, Maybe, Either, Writer, etc...); this is called lifting. You can think of it as a kind of separation of concerns.

    Mixing these two levels of abstraction doesn't facilitate legibility so you're better off just avoiding it.

提交回复
热议问题