<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body><div style="font-family: sans-serif;"><div class="plaintext" style="white-space: normal;"><p dir="auto">This is where some programmers will just reach for RuntimeException and go get their next coffee.</p>
<p dir="auto">On 14 Jun 2023, at 10:50, Brian Goetz wrote:</p>
</div><blockquote class="embedded" style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;"><div id="9DACCCD7-B296-4E8A-B02D-437B98093E71">
<font size="4"><font face="monospace">Here's how we would define this in Haskell:<br>
<br>
data Result t = Succ t | Error Throwable<br>
<br>
or maybe (Either-style)<br>
<br>
data Result t e = Succ t | Error e<br>
<br>
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. <br>
<br>
The Java analogue here is:<br>
<br>
sealed interface Result<T> { }<br>
record Succ<T>(T t) implements Result<T> { }<br>
record Fail<T>(Throwable t) implements Result<T> { }<br>
<br>
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." <br>
<br>
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. <br>
<br>
<br></font></font><br>
<div class="moz-cite-prefix">On 6/13/2023 3:17 PM, Remi Forax wrote:<br></div>
<blockquote type="cite" cite="mid:1095327035.80244534.1686683845970.JavaMail.zimbra@univ-eiffel.fr">
<pre class="moz-quote-pre" wrap="">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
</pre></blockquote>
<br></div></blockquote>
<div class="plaintext" style="white-space: normal;">
</div>
</div></body>
</html>