False negative inspector for ternaries

Issue #1147 new
Eric Kintzer created an issue

Following valid Apex code generates inspector redline

    public virtual String getTransactionId() {
        return transactionId == null 
                   ? transactionId = String.valueOf(System.currentTimeMillis()) + '_' + UserInfo.getName() 
                   : transactionId;
    }

Inspector error - ternaries.png

I'll be the first to admit that this isn't a common Apex pattern but if you're into semi-colon minimization :-) like me...

Comments (4)

  1. Scott Wells repo owner

    Thanks, Eric. Actually I use ternaries all the time and haven't seen such an issue, so I don't think it's necessarily an issue with type inference for ternary operators in general; it's likely something specific to the types of expressions being evaluated for one or more components of the ternary operator in this specific example. In fact, the error message you're getting indicates that it's a parser error and not a type inference issue. Let me take a look based on the example you've provided and hopefully it'll be obvious. At first glance I'm thinking that I'm not treating the assignment expression properly.

  2. Scott Wells repo owner

    Yep, that's what's going on. For some reason the parser is thinking that transactionId in transactionId = String.valueOf(...) is the beginning of a reference expression instead of an assignment expression. Not sure why yet...investigating now.

  3. Scott Wells repo owner

    Hmmmmm...this is trickier than I'd have expected. A fix may not materialize for a bit just because of the chance of messing up something else in the parser. The workaround until then, of course, is to extract the assignment expression. It's still very much a parser bug that I need to address, but I need to find a really safe, isolated way to do it.

  4. Eric Kintzer reporter

    Scott -- 'parser issue' - not surprising. I remember when I first posted an example of such a ternary on Salesforce Stackexchange, one of the gurus said "I didn't know you could do that (the embedded assignment)!" I understand the workaround.

  5. Log in to comment