The philosophy of Nothing
tronicek at fit.cvut.cz
tronicek at fit.cvut.cz
Sun Nov 29 22:33:41 PST 2009
They should compile because Nothing (the type inferred) is a subtype of
Integer and String.
If the type inferred was void, they should not have compiled.
So, Nothing enables the compiler to check types as it does in any other
cases.
Another example:
static <T> T m(#T() p) {
return p.invoke();
}
Integer i = m(#() { throw new AssertionError(); });
String s = m(#() { throw new AssertionError(); });
new ArrayList().add(#() { throw new AssertionError(); });
Z.
--
Zdenek Tronicek
FIT CTU in Prague
Paul Benedict napsal(a):
> Zdenk,
>
> In both cases, void or Nothing, there is no return value. So what is
> the problem you are seeing? Both examples you provided shouldn't
> compile.
>
> Paul
>
> On Mon, Nov 30, 2009 at 12:01 AM, <tronicek at fit.cvut.cz> wrote:
>> Yes! Compiler infers the return type to be able to do type checking. And
>> both of these must be correct:
>>
>> Integer i = (#() { throw new AssertionError(); }).invoke();
>> String s = (#() { throw new AssertionError(); }).invoke();
>>
>> Z.
>> --
>> Zdenek Tronicek
>> FIT CTU in Prague
>>
>>
>> Paul Benedict napsal(a):
>>> Zdenek,
>>>
>>> Is a problem present if the compiler were to infer it as void?
>>>
>>> When I put these side-by-side, I don't see any particular issue.
>>> closure #() { throw new AssertionError(); }
>>> public void something() { throw new AssertionError(); }
>>>
>>> You can write code today that never returns -- my second example.
>>>
>>> Paul
>>>
>>> On Sun, Nov 29, 2009 at 11:25 PM, <tronicek at fit.cvut.cz> wrote:
>>>> Hi Paul,
>>>>
>>>> as far as I know, Nothing is used only by compiler. And I do not see
>>>> any
>>>> benifits if I was allowed to use it as an ordinary type in my code.
>>>>
>>>> What is the primary purpose of Nothing?
>>>> Look at closure #() { throw new AssertionError(); }
>>>> Compiler must infer the return type here. But what it should be?
>>>> It can be anything because the closure never returns normally.
>>>> So, compiler will use Nothing which is a subclass of any type.
>>>>
>>>> Z.
>>>> --
>>>> Zdenek Tronicek
>>>> FIT CTU in Prague
>>>>
>>>>
>>>> Paul Benedict napsal(a):
>>>>> Thanks Neal. Reiner, your explanation was very good.
>>>>>
>>>>> So basically Nothing can be used outside of closures:
>>>>> public Nothing throwMe(RuntimeException t) {
>>>>> throw t;
>>>>> }
>>>>>
>>>>> 1) Does the Java compiler have to prove a method can never return
>>>>> normally?
>>>>>
>>>>> 2) What is the benefit of telling the compiler the method *must*
>>>>> throw
>>>>> an Exception? Don't say read the spec :-) I did, but it's sparse on
>>>>> the purpose
>>>>>
>>>>> 3) For a method that returns Nothing, if a class transformer alters
>>>>> the method body and actually issues a return, what happens? is a JVM
>>>>> Error thrown?
>>>>>
>>>>> Paul
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>>
>
More information about the coin-dev
mailing list