The issue of handling missing / alternative values with Option and friends has been a source of disappointment for quite some time, especially if we look at the alternatives other modern languages provide. The community has been throwing libraries and proposals at it (including my own proposal to empower T | Null) but it never felt like we nailed it.
And despite the grass being greener on the neighbours side, IMHO no programming language managed to provide a fully unified and satisfactory solution so far.
This proposal is really refreshing: itās ergonomic, holistic and innovative. In short, the true spirit of Scala ! And Iām sure itāll serve as a lighthouse for other languages as has been the case with a several language features in the past.
While weāre at it, we have an opportunity to take some of the best productivity features and sugars programmers love that have proven their worth across popular languages. Itās great that the postfix ? operator has already been proposed, here are some other additions that come to mind:
Feature parity with Option
fold, get, contains, exists, ⦠All the familiar methods we love from Option (and maybe others ? The Rust API could be a good source of inspiration) would make the feature more ergonomic and help with the transition.
?? AKA nullish / nil coalescing operator
A great companion to the ? operator. Languages which have the latter usually provide the former.
Available in JS / TS, Swift, C# and Kotlin (as ?:). Functionally equivalent to getOrElse but shorter and probably clearer.
Aborting to the enclosing function by default
Could we have the best of both worlds ? maybe is great when one needs that flexibility, however the most common case will probably be Rust-like, i.e. aborting to the enclosing function. Would it be feasible / desirable to have that as the default (given that the return type matches) ? This obviously would spare us some ceremony and syntax overhead (especially for one-liner functions) but mostly would make the feature easier and more accessible to beginners.
I think this would get ambiguous too quickly, since we also allow code that omits the maybe but passes a CanErr parameter (in which case the abort would go to the caller). In terms of notation itās not more verbose than Rust since Rust demands a wrapper for the result. So if we adopted Rustās scheme weād write:
So I note that JEP 401 is now in Java 28 as a preview feature. Is there anything in this feature that would likely preclude future integration with Java value classes?
Still digesting this, but from a usability standpoint, Iām mixed. Its clean, and I like the explicit T ? Err, which is less cluttered and clearer than Either (and the order doesnāt feel backwards). I worry that T? meaning T | null sends the message that ānulls are ok nowā, after years if steering users away from themāand Scala being relatively free of NPEs as a result. Maybe you save the Option wrap drama but you still need the match on result. Will the compiler warn you if you forget to match and blindly try to use result T?
Thatās why this only makes sense when paired with Explicit Nulls. As I understand it, Explicit Nulls neuters the traditional problem, which implies that it is now possible to use null in productive ways.
But thatās why it also shouldnāt be used for any serious library at this point (or soon), because only a subset of the ecosystem uses explicit nulls. If we deprecate anything but explicit nulls, then sure, use ?. But otherwise it should stay niche.
Or, alternatively, it needs to make sense without explicit nulls. Introducing a new decoration T! would do the trick, because then T for a type not provable to be disjoint from null would act akin to T | Null, T! would act like T, and T? would turn T into T! or whatever the sentinel is (which might be a no-op). So all the key pieces would be isomorphic, more or less, just accessed differently.
Todayās SIP meeting voted to accept the proposal as SIP 84 into the experimental design phase. Thereās PR 26956 with an implementation draft that makes it an experimental feature.
Once that PR is merged, people are invited to experiment with the feature and give feedback.
Was the risk for ecosystem fragmentation discussed ?
And if so what was the result ?
While this concerned has been raised here, I have not seen it being addressed by anyone in favor of this proposal
(but I might of course have missed it)
I think that to reduce the impact of ecosystem fragmentation, it would have to
Have good interop with Option and Either
Work with non-explicit nulls
Be heavily supported in the standard library from the start
The reason for the first is obvious, hopefullyāreduce the reason to pick Option and Either for new work because old libraries need those.
The second reduces fragmentation within the leading-edge ecosystem. I think there is actually nothing about T ? E that requires explicit nulls, because T as a subtype of Any which includes null just means that you are agnostic about dealing with them, while T? means that you declare you have an explicit awareness of the possibility. Thereās less point because you go from T? to T without the non-nullity of T documented in types, but it ought to at least work.
The third one gives a reason to use it rather than to stick with the old way: you gain lovely capabilities that you donāt want to miss out on. For instance, map.at("key") would be a V? so you donāt have boxing of the success case. (Whether at or something else, Iām not sure; canāt be get because thatās Option already, unless compatibility is so good that it wouldnāt even matter. But I donāt think thatās practical.)
So this could still result in a style mismatch, but I think full-on fragmentation would be rare.
Seth suggested that this might be a better venue so I will copy my comment here about the nomenclature, regarding result T or E as the way to say T ? E:
What if we pronounce this āMaybe T, otherwise Eā or somesuch? I think the result T or E naming is awkward. Firstly itās not clear whether it should be parsed mentally as (result T) or E or result (T or E). Secondly, neither really matches the construct. In the firstāwhy is E not a result? In the secondāwhat does the result wrapping of T or E meanā¦canāt you un-result it and still have the underlying T or E? (Noābecause thatās not how it works.) And then the ? sigil suggests that the two constructs are related, but the spoken form does not.
Anyway, itās very much a bike-shedding concern, but I think it is awkward to speak about as introduced here.
If we like āresultā, result T else E is probably better than āorā because T else E doesnāt sound like its own thing nearly as much as T or E does.
Note that despite being bike shedding, how you say a thing is part of the API surface, so it deserves careful attention, at least for as long as we talk about code. I suppose if LLMs are writing all code nowadays, it doesnāt really matter what itās called.
There is a potential issue with right-biased higher-kinded type inference.
Matching a type constructor F[_] with a type argument R ? E would infer [X] =>> R ? X, whereas we would usually want to have [X] =>> X ? E inferred instead.
Right biased higher-kinded type inference was arguably a design mistake caused by overfitting to the Either type and blindly copying Haskell. In Haskell, right bias makes sense because type parameters are curried, but in Scala and most other languages it is unnatural.
One could work around the problem and special case ? to use left-bias instead. But this is not proposed as part of this SIP.
I think this issue must be resolved before this is shipped. Shipping this without a solution would be outright hostile to those of us who rely on typeclass based infrastructure libraries like cats.
+1 to finding a solution for higher-kinded type inference of ?. Regardless of whether the current bias was a mistake, it is how Scala works. Having ? not work properly with HKT inference will be a baffling Scala wart to developers that run into it.
I agree with Rex that introducing the word āresultā is unnecessary and undesirable. We can just say āmaybe T, unless Eā, probably often shortened in conversation to āT unless Eā. Letās not have both āmaybe typeā and āresult typeā; more terminology muddies the waters, fewer new words keeps things clear and lean.
But note Iām suggesting āunlessā over āelseā, because it conveys a stronger flavor of E being the unhappy path.
Scala noob here. I am wondering if itās better that Null has a constructor that is not null. That way I can have a code base that has no nulls syntactically.