Cast in front of conditional seems to be invalid in javac

Roel Spilker r.spilker at gmail.com
Wed May 7 22:28:27 UTC 2014


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