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

前端 未结 20 1978
我寻月下人不归
我寻月下人不归 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:21

    This seems a bit silly to me, but the only reason I can think of is that object arguments in method parameters already are optional in a way - they can be null. Therefore forcing someone to take an existing object and wrap it in an optional is sort of pointless.

    That being said, chaining methods together that take/return optionals is a reasonable thing to do, e.g. Maybe monad.

提交回复
热议问题