Cast in front of conditional seems to be invalid in javac
Zhong Yu
zhong.j.yu at gmail.com
Thu May 8 02:47:51 UTC 2014
Relevant spec[1]:
> 15.25.3. A reference conditional expression is a poly expression if it appears in an assignment context or an invocation context (§5.2. §5.3). Otherwise, it is a standalone expression.
This means propagation of target type only works in these 2 contexts.
[1] http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25.3
On Wed, May 7, 2014 at 5:28 PM, Roel Spilker <r.spilker at gmail.com> wrote:
> Hi all,
>
> Introducing a type cast just in front of a conditional (?:) seems not to be
> valid using javac 1.8.0_05
>
> The reason to add a cast might be to create a Runnable that is also
> Serializable:
>
> Runnable r = (Runnable & Serializable) ()->{};
>
> So I would expect adding a cast to any expression would be okay.
>
> Is this a bug? If so, how/where should I register it?
>
> Roel
>
> ==== file TestCastInFrontOfConditional.java ====
>
> public class TestCastInFrontOfConditional {
> {
> Runnable works = (Boolean.TRUE ? () -> {} : () -> {});
> Runnable alsoWorks = Boolean.TRUE ? (Runnable) () -> {} : (Runnable) () ->
> {};
> Runnable fails = (Runnable) (Boolean.TRUE ? () -> {} : () -> {});
> }
> }
>
> ==== output ====
>
> TestCastInFrontOfConditional.java:6: error: lambda expression not expected
> here
> Runnable fails = (Runnable) (Boolean.TRUE ? () -> {} : ()
> -> {});
> ^
> TestCastInFrontOfConditional.java:6: error: lambda expression not expected
> here
> Runnable fails = (Runnable) (Boolean.TRUE ? () -> {} : ()
> -> {});
> ^
> 2 errors
>
> ==== end =====
>
More information about the lambda-dev
mailing list