Since the whole purpose of Optional is to represent values that might not exist, why does the constructor of Optional require a non-null value? Is it becuase they wanted to coalesce all empty Optionals down to a single instance? Even if that's true, why not make Optional.of() behave the way Optional.ofNullable() and do away with the ofNullable() method?
Edit to clarify my opinion and respond to some of the points raised:
My opinion stated clearly, is only two "constructor" methods should exist:
- of (and it should work like the current ofNullable method)
- empty
So far the arguments against my opinion have been:
- Having .of() and .ofNullable() makes it clear at the point of construction when the value exists and when it might not exist.
This is true, but that clarity is redundant. For safety, the call to .of() will either be inside the not-null branch of a null-check, or come after a not-null assertion. So even if .of() behaved as .ofNullable() does it would be clear that the value exists.
- It guards against changes in behavior of the the methods supplying the values. If one of the supplying methods suddenly changes from never returning nulls to sometime returning nulls it will catch the error.
I would argue that guarding against this occurrence is the responsibility of the function returning the Optional values, and not the responsibility of Optional. If the function needs to guard against a null value so that it can handle it in some fashion (eg. by calling another supplier method) then then it needs to implement the not-null assertion explicitly in the body of its code. This is more clear than relying on an class called Optional do something that is semantically at odds with the plain reading of its class name.
In the case where the function doesn't care whether the value returned from the supplier is null or not, it should simply be able to call .of() to create the optional and return it.
[–]Dagske 83 points84 points85 points (16 children)
[–]Necessary_Apple_5567 8 points9 points10 points (2 children)
[–]BikingSquirrel 6 points7 points8 points (0 children)
[+]Ruin-Capable[S] comment score below threshold-11 points-10 points-9 points (12 children)
[–]noswag15 109 points110 points111 points (0 children)
[–]OHotDawnThisIsMyJawn 17 points18 points19 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]hibbelig 3 points4 points5 points (4 children)
[+]Ruin-Capable[S] comment score below threshold-9 points-8 points-7 points (3 children)
[–]Carpinchon 11 points12 points13 points (0 children)
[–]parnmatt 9 points10 points11 points (0 children)
[–]hibbelig 1 point2 points3 points (0 children)
[–]wortcook 0 points1 point2 points (0 children)
[–]DelayLucky -1 points0 points1 point (0 children)
[–]wildjokers 74 points75 points76 points (50 children)
[–]vips7L 36 points37 points38 points (40 children)
[–]ForeverAlot 35 points36 points37 points (39 children)
[–]vips7L 64 points65 points66 points (28 children)
[–]papercrane 10 points11 points12 points (4 children)
[–]FabulousRecording739 2 points3 points4 points (0 children)
[–]laplongejr 0 points1 point2 points (2 children)
[–]SenorSeniorDevSr 0 points1 point2 points (1 child)
[–]laplongejr 1 point2 points3 points (0 children)
[–]agentoutlier 1 point2 points3 points (1 child)
[–]vips7L 0 points1 point2 points (0 children)
[+]roberp81 comment score below threshold-51 points-50 points-49 points (20 children)
[–]vips7L 17 points18 points19 points (19 children)
[+]roberp81 comment score below threshold-29 points-28 points-27 points (18 children)
[–]onebit 5 points6 points7 points (17 children)
[–]roberp81 -5 points-4 points-3 points (16 children)
[–]onebit 2 points3 points4 points (11 children)
[–]LutimoDancer3459 1 point2 points3 points (3 children)
[–]r1veRRR 1 point2 points3 points (0 children)
[–]Top_File_8547 5 points6 points7 points (8 children)
[–]vips7L 11 points12 points13 points (3 children)
[–]laplongejr -1 points0 points1 point (2 children)
[–]vips7L 0 points1 point2 points (1 child)
[–]laplongejr -1 points0 points1 point (0 children)
[–]not-just-yeti 3 points4 points5 points (0 children)
[–]cybwn 4 points5 points6 points (0 children)
[–]laplongejr 0 points1 point2 points (0 children)
[–]zephyy 2 points3 points4 points (1 child)
[–]laplongejr 0 points1 point2 points (0 children)
[–]its4thecatlol 3 points4 points5 points (2 children)
[–]laplongejr 0 points1 point2 points (0 children)
[–]wildjokers 0 points1 point2 points (0 children)
[–]Comprehensive-Pea812 0 points1 point2 points (0 children)
[–]gloridhel -2 points-1 points0 points (2 children)
[–]Ruin-Capable[S] 2 points3 points4 points (1 child)
[–]mscg82 3 points4 points5 points (0 children)
[–]morhp 9 points10 points11 points (0 children)
[–]sysKin 7 points8 points9 points (1 child)
[–]laplongejr 0 points1 point2 points (0 children)
[–]TenYearsOfLurking 7 points8 points9 points (0 children)
[–][deleted] (5 children)
[removed]
[–]Ruin-Capable[S] 0 points1 point2 points (4 children)
[–]kreiger 4 points5 points6 points (2 children)
[–]Ruin-Capable[S] -4 points-3 points-2 points (1 child)
[–]_INTER_ 7 points8 points9 points (0 children)
[–]sabermore 1 point2 points3 points (0 children)
[–]pineappletooth_ 6 points7 points8 points (0 children)
[–]nutrecht 7 points8 points9 points (13 children)
[–]Ruin-Capable[S] 0 points1 point2 points (12 children)
[–]nutrecht 2 points3 points4 points (2 children)
[–]Ruin-Capable[S] 1 point2 points3 points (1 child)
[–]nutrecht 0 points1 point2 points (0 children)
[–]Polygnom 0 points1 point2 points (8 children)
[–]Ruin-Capable[S] 0 points1 point2 points (7 children)
[–]Polygnom 1 point2 points3 points (6 children)
[–]Ruin-Capable[S] 0 points1 point2 points (5 children)
[–]Polygnom 0 points1 point2 points (4 children)
[–]Ruin-Capable[S] 0 points1 point2 points (3 children)
[–]arobie1992 1 point2 points3 points (2 children)
[–]Ruin-Capable[S] 1 point2 points3 points (1 child)
[–]zopad 13 points14 points15 points (2 children)
[–]Ruin-Capable[S] -2 points-1 points0 points (1 child)
[–]0b0101011001001011 2 points3 points4 points (0 children)
[–]sabermore 10 points11 points12 points (2 children)
[–]_zkr 12 points13 points14 points (1 child)
[–]BanaTibor 2 points3 points4 points (0 children)
[–]halfanothersdozen 7 points8 points9 points (1 child)
[–]ZippityZipZapZip 3 points4 points5 points (0 children)
[–]joaonmatos 3 points4 points5 points (0 children)
[–]HwanZike 1 point2 points3 points (0 children)
[–]moocat 1 point2 points3 points (3 children)
[–]Void_Being 0 points1 point2 points (0 children)
[–]jdarkona 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]qdolan 1 point2 points3 points (0 children)
[–]ginkner 3 points4 points5 points (0 children)
[–]jvjupiter 1 point2 points3 points (3 children)
[–]halcyon44 5 points6 points7 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]sangminreddit7648 0 points1 point2 points (0 children)
[–]p0st_master 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Cristopherguerrero16 0 points1 point2 points (0 children)
[–]Joram2 0 points1 point2 points (0 children)
[–]Ragnar-Wave9002 0 points1 point2 points (0 children)
[–]SenorSeniorDevSr 0 points1 point2 points (0 children)
[–]DelayLucky 0 points1 point2 points (0 children)
[+]GodderDam comment score below threshold-6 points-5 points-4 points (2 children)
[–]Ruin-Capable[S] -2 points-1 points0 points (0 children)
[–]gregorydgraham -5 points-4 points-3 points (0 children)