PROPOSAL: Elvis operator

Olivier Chorier lapsus63 at gmail.com
Sat Mar 21 09:47:10 PDT 2009


Oh, ok, I get it :
Ex. : int value = object.getSubObject().getValue() ?: -1 ;

So, I'm not sure the use of .? to be necessary.

What do you think about an explicit ((MyObject) null)... ?: defValue;

Marek told:

*<<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;>>*
--> "default" would have to be cast as SubObject<FirstType, SecondType,
ThirdType>, wouldn't it ?

Finally, another idea could be not to use '?:' neither '.?' but a reversed
way to use:
int value = object?.getSubObject()?.getValue()? : -1

- object null ? return -1;
- object.getSubObject() null ? return -1;
- object.getSubObject().getValue() null ? return -1;

But it implies the multiple use of '?', which is in my opinion not very sexy
(but more detailed and "configurable")



2009/3/21 Marek Kozieł <develop4lasu at gmail.com>

> W dniu 21 marca 2009 16:00 użytkownik Olivier Chorier
> <lapsus63 at gmail.com> napisał:
> >
> > 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
> >
>
> It's ok for me, in this case.
>
> But did you ever wander why NPE is so common? Isn't that because
> people do not want write so much if-s and declare so many one time
> used variables.
>
> but ?: can give something nice:
>   void f(Some some){
>      ( some.getOut ?: defOut ).write(...);
>      ...
>   }
>
> while ?= would require to:
>   void f(Some some){
>     OutputStream out ?= some.getOut : defOut;
>     out.write(...);
>     // Now we need worry for 'out' till end of function.
>     ...
>   }
>
> --
> Pozdrowionka. / Regards.
> Lasu aka Marek Kozieł
>
> http://lasu2string.blogspot.com/
>
>



More information about the coin-dev mailing list