PROPOSAL: Elvis operator
Marek Kozieł
develop4lasu at gmail.com
Sat Mar 21 07:23:32 PDT 2009
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