samskivert
samskivert » Thank Gosling

November 4, 2009

Thank Gosling

The following is not legal Java:

    public static void main (String[] args) {
        int[] foo = new int[1];
        int[] bar = new int[1];
        ((args.length == 0) ? foo[0] : bar[0]) = 1;
    }

That makes my life easier.

Posted to code by mdb at 5:25 pm | Comments (2)       

2 Responses to “Thank Gosling”

  1. wtfwtf_ok says:

    The 2nd and 3rd operands to the ternary operator must be values, not variables.

    Your particular example could be done similarly as:

    // legal
    ((args.length == 0) ? foo : bar)[0] = 1;

    But:

    // not legal
    ((args.length == 0) ? foo : bar) = new int[1];

  2. mdb says:

    Yeah, that turns out to be very useful for my current purposes, which is that I have an AST and I’m looking at the assignment operator and I want to know of the left-hand side is an array select or not. Clearly the left child of the AST could just be an array select and I’m fine, but I was wondering when I might need to traverse the left-tree to go looking for a select.

    I believe that the only time I need to do that is if the left-hand side is a Paren node. The following is legal:

    (foo[3]) = 5;

    But AFAIK the only thing I need to worry about is parens. No other expression on the left-hand side can evaluate to a variable into which I will store a value. Probably I’m missing something. :)

Leave a Reply

You must be logged in to post a comment.

MDB

Bits
Ludography
Reviewlets
Camera Wrestling
Archives:

samskivert.com ©2001 - 2009 Michael Bayne <mdb@samskivert.com>