Meta tags:
description= A digest of how Optional was introduced in Java 8, summarizing the many discussions about it and their key points based on the mail archive of JSR-335.;
keywords= Optional;
Headings (most frequently used words):
the, optional, design, of, jsr, 335, road, to, reflection, first, blood, endless, discussions, convergence, simplicity, value, type,
Text of the page (most frequently used words):
the (127), #optional (75), java (51), and (47), that (45), for (30), value (29), this (28), not (28), there (21), which (19), would (18), with (17), return (17), like (17), was (17), but (16), use (16), null (16), about (15), can (14), should (14), project (14), some (13), type (13), they (13), 2013 (12), more (11), only (10), are (10), post (9), group (9), will (9), were (9), one (9), you (8), collections (8), here (8), reason (8), design (8), those (8), discussions (8), code (8), class (8), get (8), people (8), findfirst (8), have (7), expert (7), just (7), then (7), from (7), when (7), mar (7), stream (7), lambda (7), brian (6), goetz (6), share (6), values (6), jsr (6), user (6), doug (6), lea (6), methods (6), tim (6), peierls (6), map (6), 335 (6), does (5), don (5), default (5), might (5), future (5), way (5), another (5), very (5), what (5), reflection (5), list (5), operations (5), whether (5), object (5), nothing (5), all (5), new (5), their (5), make (5), first (5), result (5), its (5), into (5), junit (5), license (4), want (4), discussion (4), your (4), over (4), them (4), has (4), same (4), other (4), case (4), last (4), think (4), even (4), empty (4), many (4), free (4), things (4), users (4), likely (4), two (4), classes (4), identity (4), based (4), without (4), added (4), 2012 (4), usage (4), everyone (4), key (4), these (4), part (4), context (4), streams (4), community (4), oct (3), why (3), related (3), opinion (3), how (3), lead (3), being (3), see (3), allow (3), such (3), instead (3), led (3), something (3), either (3), cases (3), different (3), often (3), everywhere (3), opinions (3), out (3), say (3), mailing (3), unpredictable (3), serialization (3), any (3), end (3), could (3), still (3), variants (3), sometimes (3), possible (3), works (3), introduced (3), idea (3), sep (3), really (3), hard (3), using (3), year (3), others (3), scala (3), simplicity (3), convergence (3), enough (3), mentioned (3), style (3), api (3), both (3), between (3), forax (3), need (3), endless (3), apis (3), blood (3), road (3), libs (3), experts (3), language (3), expressions (3), features (3), give (3), follow (3), posts (3), series (3), next (3), pioneer (3), demos (3), core (3), clean (3), privacy (2), rss (2), stackoverflow (2), github (2), twitch (2), youtube (2), discord (2), mastodon (2), bluesky (2), take (2), messages (2), back (2), let (2), must (2), extract (2), missing (2), google (2), quick (2), created (2), immediately (2), especially (2), box (2), handed (2), again (2), library (2), call (2), avoid (2), situations (2), whatever (2), recommend (2), ignored (2), represent (2), avoided (2), trying (2), better (2), than (2), least (2), break (2), wrong (2), small (2), most (2), work (2), now (2), versions (2), sensitive (2), may (2), effects (2), via (2), reference (2), lengthy (2), definition (2), contains (2), means (2), factory (2), spec (2), every (2), much (2), note (2), version (2), primitives (2), puts (2), his (2), types (2), limited (2), goal (2), simple (2), kevin (2), guava (2), reasonable (2), hashcode (2), equals (2), providing (2), been (2), time (2), had (2), parameter (2), mean (2), going (2), string (2), currently (2), testing (2), alternative (2), functionality (2), support (2), needed (2), contain (2), fluent (2), collection (2), system (2), option (2), orelse (2), came (2), made (2), survey (2), neither (2), feature (2), otherwise (2), enable (2), completely (2), plain (2), findany (2), easily (2), able (2), decide (2), sentence (2), fact (2), agreed (2), returning (2), provided (2), prevents (2), distinguishing (2), nearly (2), defaultvalue (2), create (2), necessary (2), terminal (2), compromise (2), hold (2), who (2), unhappy (2), hope (2), seems (2), sides (2), discourse (2), discussed (2), happy (2), although (2), valueifempty (2), rémi (2), mail (2), reasoning (2), archive (2), source (2), well (2), active (2), various (2), platforms (2), watch (2), space (2), notified (2), publish (2), content (2), insight (2), misunderstood (2), misused (2), serialize (2), isn (2), serializable (2), intention (2), revealing (2), talks (2), libfx (2), record (2), args (2), var (2), turn (2), tools (2), techniques (2), switch (2), records (2), valhalla (2), panama (2), loom (2), leyden (2), amber (2), performance (2), patterns (2), pattern (2), matching (2), ramp (2), migration (2), meta (2), maven (2), libraries (2), javafx (2), basics (2), j_ms (2), generics (2), dop (2), documentation (2), deprecation (2), architecture (2), tags (2), javascript (2), contact, imprint, sigh, everything, 300, join, comment, below, answer, ping, careful, bolivian, tree, lizard, deal, ones, separately, sure, ignore, guide, concepts, apply, handle, specific, following, principle, mostly, reflexively, actual, absence, change, logical, flow, point, exists, final, argument, around, until, moment, dealing, though, feasible, common, ground, compared, deliberately, decisions, having, representation, necessity, thing, clearer, path, definitely, taken, accomplish, programmer, look, solution, sprinkling, short, clearly, wishes, whereas, also, lookout, ended, judging, broke, provide, cover, adequately, warned, discourage, doing, behave, special, pleading, motivations, defines, battery, done, instances, program, produce, results, attempts, distinguish, references, equal, directly, equality, indirectly, appeal, synchronization, hashing, mechanism, inform, javadoc, contained, remark, includes, link, warning, term, guarantees, optionals, tells, block, forever, myoptional, synchronized, myoptional1, myoptional2, guarantee, unique, objects, require, magical, implementation, disallowed, itself, public, constructor, evolve, write, codes, int, referring, concept, gross, simplification, define, kind, interfaces, central, characteristic, handled, introductory, article, state, feb, stay, lean, easier, day, optimize, away, extra, participant, kiss, optimizations, goodbye, besides, limit, keep, mike, duigou, talked, bourrillion, member, experiences, response, felt, obligatory, eventually, curse, implementations, grudging, reluctance, prevent, rage, obfuscating, conveys, exactly, returns, callable, answered, david, lloyd, feared, discouragement, abstraction, good, holding, potential, presence, retrieving, present, resist, temptation, knock, off, similar, adding, broad, consensus, kept, too, discouraged, slowly, converged, variant, tail, example, embedded, implementing, iterable, filter, ifpresent, bearing, tie, favor, opposed, abstained, seemed, voters, misconception, rid, unreliable, strangely, nor, repeated, did, overlook, foremost, allowing, adopt, rather, put, npes, downstream, blame, aggreed, surprisingly, wanting, derived, allmatch, etc, hints, cited, pair, distinguished, caller, serious, shortcoming, noted, liked, forbid, always, signal, containing, mapped, didn, sam, pullara, removing, doesn, written, right, tell, anyway, wart, supplier, fixed, remi, because, letting, returned, voiced, particular, quote, forgo, jun, several, poles, whose, proponents, strong, feel, hack, little, except, vain, pet, elvis, boy, discuss, generating, hundreds, long, exchanges, opposing, shaped, reflected, already, held, sole, solely, described, queries, precisely, max, min, reduce, motto, win, sake, less, realize, optionalism, starts, propagating, through, designs, leading, set, extended, boxed, prefer, elements, cannot, unambiguously, sense, lot, years, mainly, amount, technical, problems, plus, issue, prize, mentioning, september, gave, overview, behind, found, text, layout, details, bold, face, links, members, strongly, involved, multiple, chiming, known, joshua, bloch, inclusion, extend, compact, closures, effectively, dealt, programming, specification, request, main, summary, process, introduction, speak, themselves, quoting, wherever, properly, convey, frequent, controversial, heated, trivial, task, contrast, own, turns, odds, curious, read, creation, decided, findings, promoted, replacement, toc, ser, table, contents, lic, artist, dev, src, digest, summarizing, points, 2014, slides, upcoming, past, schedule, recordings, virtual, threads, vector, structured, concurrency, lilliput, galahad, babylon, openjdk, conversation, book, club, videos, jms, newsletter, blog, rant, jigsaw, jdeps, impulse, lang, review, comments, words, site, enabled, nipafx,
Text of the page (random words):
18 java 20 java 23 java 24 java 25 java 26 java 27 java 8 java 9 java basics java next javafx jdeps js junit 5 junit pioneer lambda libfx libraries maven meta migration on ramp optional pattern matching patterns performance project amber project jigsaw project leyden project loom project panama project valhalla rant record args records reflection serialization streams switch techniques testing tools turn of the year var blog posts newsletter the jms videos tags ai architecture book club clean code collections community conversation core libs deprecation documentation dop generics j_ms java 10 java 11 java 12 java 16 java 17 java 18 java 19 java 21 java 22 java 23 java 24 java 25 java 26 java 27 java 28 java 8 java 9 java basics java next javafx junit 5 junit pioneer lambda libraries maven meta migration on ramp openjdk optional pattern matching patterns performance project amber project babylon project galahad project leyden project lilliput project loom project panama project valhalla records reflection serialization streams structured concurrency switch techniques tools turn of the year var vector virtual threads recordings streams schedule code demos demos demos junit pioneer record args libfx talks my talks past upcoming slides about about me license privacy 2014 10 18 the design of optional post java next java 8 optional a digest of how optional was introduced in java 8 summarizing the many discussions about it and their key points based on the mail archive of jsr 335 src source dev artist lic license the design of optional series this post is part of a series intention revealing code with optional the design of optional this one why isn t optional serializable serialize optional these posts give you more insight into one of java s more misunderstood and misused features table of contents jsr 335 the road to optional first blood endless discussions convergence simplicity value type reflection share follow share this post with your community i m active on various platforms watch this space or follow me there to get notified when i publish new content ser this post is part of a series intention revealing code with optional the design of optional this one why isn t optional serializable serialize optional these posts give you more insight into one of java s more misunderstood and misused features toc jsr 335 the road to optional first blood endless discussions convergence simplicity value type reflection s f share this post with your community i m active on various platforms watch this space or follow me there to get notified when i publish new content in my last post i promoted using java 8 s new type optional nearly everywhere as a replacement for null as it turns out this puts me at odds with with the expert group which introduced the type this made me curious so i read up on its creation and decided to share my findings here the main part of this post is trying to give a summary of the process which lead to the introduction of optional in java 8 i will let the experts speak for themselves by quoting them wherever possible i hope to properly convey the discourse but as the discussions were frequent lengthy and sometimes controversial and heated this is no trivial task at the end i will contrast the expert group s reasoning with my own jsr 335 the java specification request 335 dealt with lambda expressions for the java tm programming language its goal was extend the java language to support compact lambda expressions closures as well as related language and library features to enable the java se apis to use lambda expressions effectively it was this context which lead to the inclusion of optional in java 8 members of the expert group for jsr 335 and strongly involved in the multiple discussions about optional were people like brian goetz doug lea and rémi forax chiming in were known experts like joshua bloch tim peierls and others the archive of the mailing list lambda libs spec experts is the source for this post it can be found here as it is plain text all layout details like bold face or links were added by me the road to optional first blood the prize for first mentioning optional back in september 2012 seems to go to rémi forax although it was doug lea who cc ed the group s mailing list in this mail he gave a quick overview over the reasoning behind the possible need for a new type there has been a lot of discussion about optional here and there over the years i think they mainly amount to two technical problems plus at least one style usage issue some collections allow null elements which means that you cannot unambiguously use null in its otherwise only reasonable sense of there s nothing there if when some of these apis are extended to primitives there is no value to return in the case of nothing there the alternative to optional is to return boxed types which some people would prefer not to do some people like the idea of using optional to allow more fluent apis as in x s findfirst or valueifempty vs if x s findfirst null x valueifempty some people are happy to create an object for the sake of being able to do this although sometimes less happy when they realize that optionalism then starts propagating through their designs leading to set optional t s and so on it s hard to win here doug lea sep 14 2012 by the way if there were a motto for the discussions about optional the last sentence would be it note that optional is solely described as a return type for queries to a collection which was discussed in the context of streams more precisely it was needed for those terminal operations which can not return a value if the stream is empty currently those are reduce min max findfirst and findany it is hard to say whether that shaped the future discourse or just reflected the opinions already held but it came to be the sole context in which optional was discussed as a type for return values endless discussions from then on optional created long exchanges and opposing sides every time it was mentioned and not just two sides either boy it seems that one can t discuss optional in any context without it generating hundreds of messages whatever we do here is a compromise between several poles whose proponents hold very strong opinions there are those that really want elvis instead there are others who feel that a box like class for optional is a hack when it really should be part of the type system neither group is going to get what they want here we can compromise and make everyone a little unhappy or we can do nothing and make everyone unhappy except that they will still hold out vain hope for their pet feature in the future brian goetz jun 5 2013 an often voiced opinion not mentioned in that particular quote was to forgo optional completely in fact we don t need optional at all because we don t need to return a value that can represent a value or no value the idea is that methods like findfirst should take a lambda as parameter letting the user to decide what value should be returned by findfirst if there is a value and if there is no value remi forax mar 6 2013 so terminal operations which can not return a value if the stream is empty should in that case return a user provided value so instead of this optional t findfirst it would be one or both of these return a fixed default value if necessary t findfirst t defaultvalue create a default value if necessary t findfirst supplier defaultvalue some agreed i am for removing optional if it doesn t have nearly the same functionality as the scala option the way optional is written right now i would tell people not to use it anyway and it would just be a wart on this api sam pullara mar 6 2013 some didn t returning the user provided default value prevents people from distinguishing between a stream that is empty and a stream containing only the orelse value just like map get prevents distinguishing between not there and mapped to null brian goetz mar 6 2013 the last sentence hints at an often cited case the fact that map get object key can return null which can either mean that the map contains the pair key null or that it does not contain the key both cases are not easily distinguished by the caller everyone on the list agreed that this was a serious shortcoming of the map api most noted that they would have liked all collections to forbid null as a value like many guava collections do so returning null could always signal nothing there another opinion about whether to return optional or not was to have both variants then the users would be able to decide whether they want to use optional or not people wanting to avoid optional can then then get all of the derived versions allmatch plain findany etc easily enough surprisingly enough that s the only missing feature that would otherwise enable a completely optional free usage style of the stream api doug lea mar 6 2013 but not everyone aggreed the foremost reason i see for not allowing an optional free usage style is that people will adopt it rather than use optional they will see it as a license to put null everywhere and they ll get npes way downstream and blame it on java tim peierls mar 6 2013 a survey about whether the not optional bearing variants should be added came to a tie of 3 in favor 3 opposed and 1 abstained but it seemed that some voters had the misconception that they could still get rid of optional which made the result unreliable strangely enough the survey was neither mentioned again nor repeated or did i overlook something convergence but the discussion slowly converged optional would be the return value of those stream operations which needed it and there would be no optional free variant it would contain some methods for fluent usage at the tail end of stream operations like ifpresent orelse filter and map but not much more for example would it not be embedded into the collection system by implementing iterable like scala s option simplicity the reason for not adding more functionality was a broad consensus that optional should be kept simple and not support too many different use cases especially its use in collections should be discouraged optional should be and currently is a very limited abstraction one that is only good for holding a potential result testing for its presence retrieving it if it is present and providing an alternative if not we should resist the temptation to make it into something more or make it into a knock off of the similar scala type tim peierls mar 6 2013 others feared that any discouragement would be ignored i don t like it i think it s going to result in things like map string optional list optional string david m lloyd sep 14 2012 which was answered only if you really work hard at obfuscating your code i ve been using a version of optional for about a year and the only time i had reason to use optional as a type parameter was callable optional result which conveys exactly what i mean might have a result when it returns tim peierls sep 14 2012 even equals hashcode were only added to prevent user rage we talked to kevin kevin bourrillion from google member of the expert group about their experiences with guava s optional his response was that they felt reasonable hashcode equals methods were obligatory and without them users would if not immediately then eventually curse us for not providing them the implementations are added with grudging reluctance mike duigou mar 8 2013 value type besides the goal to limit optional s use there was another reason to keep the class simple here s another reason to stay lean the more limited optional is the easier it will be some day to optimize away the extra object make it a first class participant and you can kiss those optimizations goodbye tim peierls feb 26 2013 what tim peierls is referring to is the concept of value types which will very likely be introduced in some future version of java the gross simplification of that idea is that the user can define a new kind of type different from classes and interfaces their central characteristic is that they will not be handled by reference like classes but by value like primitives or as brian goetz puts it in his introductory article state of the values codes like a class works like an int that java would likely evolve that way led doug lea to write this note that optional is itself a value like class without a public constructor just factory methods the factory methods do not even guarantee to return unique objects for all that the spec does and should say every call to optional of could return the same optional object this would require a magical implementation but still not disallowed and variants that sometimes return the same one are very much possible this means that there are no object identity related guarantees for optionals myoptional1 myoptional2 tells you nothing and synchronized myoptional has unpredictable effects it might block forever doug lea oct 19 2013 this led to another lengthy discussion about how to inform the user about that at the end optional s and other class javadoc contained a small remark that it is a value based class which includes a link to the term s definition that definition contains this warning a program may produce unpredictable results if it attempts to distinguish two references to equal values of a value based class whether directly via reference equality or indirectly via an appeal to synchronization identity hashing serialization or any other identity sensitive mechanism use of such identity sensitive operations on instances of value based classes may have unpredictable effects and should be avoided this defines a small battery of things which must not be done on those classes they would most likely work for now but might break in future versions the users are more likely to behave but the special pleading has two motivations discourage users from doing wrong things provide cover so that when we break code that does wrong things they were adequately warned brian goetz oct 23 2013 and with that lookout ended the discussions about optional at least for the jsr but judging from the opinions out there i d say it just broke free from that mailing list reflection in short the expert group clearly wishes us to only use optional as a type for return values whereas i recommend to also use it in other situations but i think we share some common ground i only compared optional to null and deliberately ignored the design decisions which led to something not being there even having a representation either null or an empty optional in many cases the necessity to represent such a thing can be avoided with a different often clearer design a path which should definitely be taken and i think this is what the expert group is trying to accomplish have the programmer look for a better solution than sprinkling optional everywhere there might be situations though were such a design is not feasible for whatever reason and in those and only those i recommend t...
|