Fwd: PROPOSAL: Elvis operator (original) (raw)

Olivier Chorier lapsus63 at gmail.com
Sat Mar 21 08:04:41 PDT 2009


---------- Forwarded message ---------- From: Olivier Chorier <lapsus63 at gmail.com> Date: 2009/3/21 Subject: Re: PROPOSAL: Elvis operator To: Marek Kozieł <develop4lasu at gmail.com>

using : ...using a loop (an unexpected shortcut sent the mail sorry)

2009/3/21 Olivier Chorier <lapsus63 at gmail.com>

Ok, what about :

int value ?= object.getSubObject().getValue() : -1; Pros : - I think the compiler could easily retranscript it using - Much more readable Cons : - The potential problem is that the "interpretation" differs from an &= or += operator 2009/3/21 Marek Kozieł <develop4lasu at gmail.com> 2009/3/21 Olivier Chorier <lapsus63 at gmail.com>

I initially thought it was a good idea, but after a bit reflection, I don't think it is a real 'plus' compared with the ternary operator.

For example, how would you simplify those lines of code (assuming the coder loves ternary operator) : int value = object == null ? -1 : (object.getSubObject() == null ? -1 : (object.getSubObject().getValue() == null ? -1 : object.getSubObject().getValue())); Does somebody has an idea (excepting using an ugly try-catch statement) to write this much clearer ? (sorry for code indentation)

2009/3/21 Stephen Colebourne <scolebourne at joda.org>

You wrong, this should look like: int value = object.?getSubObject().?getValue() ?: -1 But there is another problem that I would like to see handled: What if we have default SubObject which we can use when main one is null, and it's ugly generic: SubObject<FirstType,SecondType,TirdType,.......> subObject = object.?getSubObject() ?: default; int value = subObject.?getValue() ?: -1; // here we can use subObject now same in other syntax: int value = object.?getSubObject() ?:( subObject ) .?getValue() ?: -1; and other syntax: object.?getSubObject() :: subObject; int value = ( subObject ?: default ).?getValue() ?: -1; // here we can use subObject and other: int value = ( object.?getSubObject() ?: default ).?getValue() ?: -1; Consider witch is easier to read. -- Pozdrowionka. / Regards. Lasu aka Marek Kozieł http://lasu2string.blogspot.com/



More information about the coin-dev mailing list