Cast in front of conditional seems to be invalid in javac
Roel Spilker
r.spilker at gmail.com
Mon May 19 12:33:56 UTC 2014
Thanks for the pointer.
Is there a good reason why this is the spec?
What would change if a next version of the spec will be modified to also
allow a casting context, as is currently the case for method references[1]
[1] http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.13.2
On Thu, May 8, 2014 at 4:47 AM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
> 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