Money for Nothing, ...
John Rose
john.r.rose at oracle.com
Wed Jun 14 18:38:25 UTC 2023
This is where some programmers will just reach for RuntimeException and
go get their next coffee.
On 14 Jun 2023, at 10:50, Brian Goetz wrote:
> Here's how we would define this in Haskell:
>
> data Result t = Succ t | Error Throwable
>
> or maybe (Either-style)
>
> data Result t e = Succ t | Error e
>
> Note that in either, `Error` is still generic in `t`, its just that it
> doesn't use `t`, so it doesn't say `t`. Similarly, in the latter,
> both alternatives are generic in both tvars, but each uses only one of
> them.
>
> The Java analogue here is:
>
> sealed interface Result<T> { }
> record Succ<T>(T t) implements Result<T> { }
> record Fail<T>(Throwable t) implements Result<T> { }
>
> What you are probably reacting to here is "but why does Fail have to
> say T, it doesn't use it." And the answer is: "get over that, and
> then you're done."
>
> You are trying to invent a new generics feature to avoid putting a `T`
> you don't use in your Error declaration. But that T (and maybe E)
> are needed to unify the two under Result<T,E> --
> just like in the second Haskell example above.
>
>
>
> On 6/13/2023 3:17 PM, Remi Forax wrote:
>> Hello,
>> currently, it's not possible to write a lot of generics sealed type
>> because Java has no way to denote the bottom type.
>>
>> By example, if a Result can be either a Success or an Error, we want
>> to be able to write this kind of switch
>>
>> public static void main(String[] args) {
>> Result<String, IOException> result = ...
>> var val = switch(result) {
>> case Error<IOException> error -> 1;
>> case Success<String> success -> 2;
>> };
>> }
>>
>> But i do not see a way to do that without introducing a way to denote
>> the bottom type (named "Nothing" here)
>>
>> sealed interface Result<T,E extends Exception> {}
>> record Error<E extends Exception>(E error) implements
>> Result<Nothing, E> {}
>> record Success<T>(T value) implements Result<T, Nothing> { }
>>
>> Nothing being the return type of a method that either never terminate
>> (by example, using a for(;;)) or always throw an exception.
>>
>> So, should we add Nothing to Java or is there another way to model
>> this kind of sealed types ?
>>
>> regards,
>> Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20230614/b24c7968/attachment.htm>
More information about the amber-spec-experts
mailing list