Shouldnt Optional be Serializable-()

  本篇文章为你整理了Shouldnt Optional be Serializable?()的详细内容,包含有 Shouldnt Optional be Serializable?,希望能帮助你了解 Shouldnt Optional be Serializable?。

  

Amplifying Mikes observations: this exploration is already covered ground. The JSR-335 EG felt fairly strongly that Optional should not be on any more than needed to support the optional-return idiom only. (Someone suggested maybe even renaming it to OptionalReturn to beat users over the head with this design orientation; perhaps we should have taken that suggestion.) I get that lots of people want Optional to be something else. But, its not simply the case that the EG forgot to make it serializable; they explicitly chose not to. And theres certainly been no new data that has come to light that would motivate us to set that choice aside at this point. 

 

  
Presumably because you may want to have class fields that express

   nullability via Optional rather than null.

   Using Optional as a field type doesnt seem to offer much.

   Would the Optional field be wrapped in accessors?

   If its not wrapped, the result is that orElse(default) gets spread across the usage of the field which is a poor way to apply a default.

   If if it is wrapped, why not apply the default in the accessor or in the setter and not bother with Optional?

   Whether thats a good design or

   not is a separate question;

   Thats not generally how JDK expert groups think. Adding any feature to intentionally support bad design wouldnt make much sense. Understanding the dimensions of how a feature might be used is certainly something that the EG spends a lot of time on. The choice of semantics, methods offered and interfaces implemented isnt ever separated from the expected use cases. Optional is a new class/feature to the JDK and it seemed appropriate to start with a minimal implementation. Concern that Optional would be misused in other use cases threatened to derail its inclusion in Java entirely! Optional is being added for the value it offers in fluent sequences of statements. In this context use of Optional as a visible type or for serialization isnt relevant.

   conceptually, I dont see a reason why Optional

   cannot support that. For reference , Google Guavas version is

   serializable.

   Guavas Optional certainly proves that it is technically possible (which was probably already known anyway) but that doesnt address whether doing so with the Java Optional would be a good idea or not.

   If someone were to replace their use with jdks Optional

   then they will hit exceptions if the owner class is serialized.

   While the incompatibility is certainly unfortunate this is probably not a sufficient reason by itself to make Javas Optional serializable.

   Mike

   Sent from my phone

   On Sep 17, 2013 6:06 PM, Remi Forax forax at univ-mlv.fr wrote:

   On 09/17/2013 11:44 PM, Pete Poulos wrote:

   Shouldnt java.util.Optional be Serializable? Is there a good reason for

   it not be?

  以上就是Shouldnt Optional be Serializable?()的详细内容,想要了解更多 Shouldnt Optional be Serializable?的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: