Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 368220

Summary: [Xbase] elvis "operator"
Product: [Modeling] TMF Reporter: Sven Efftinge <sven.efftinge>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: mckinley1411, sebastian.zarnekow
Version: 2.3.0Flags: sebastian.zarnekow: juno+
Target Milestone: Final   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Sven Efftinge CLA 2012-01-10 03:03:51 EST
The elvis operator effectively allows to provide a default value in case an expression evaluates to 'null'.

Example :

  someStringOrNull :? 'hello'

I propose to do this via library using an extension method on java.lang.Object.

  someStringOrNull.ifnull['hello']

@rrmckinley suggested via twitter to use a shorter more readable name like 'or' or 'else'. 
Unfortunately 'else' being a keyword won't work and 'or' could be surprising since as it would be defined on Object the following expression would result in 'false':

false.or[true]

Any other suggestions?
Comment 1 McKinley CLA 2012-01-10 03:14:31 EST
I agree with you that 'or' is a bad surprise. I rescind! ;-) Null should be in the method name to be the best guide.
Comment 2 Sebastian Zarnekow CLA 2012-01-10 04:24:47 EST
I find ifNull the most intuitive name. IMO 'else' or 'or' are not descriptive enough and I don't think that we really need a shorter name than ifNull.
Comment 3 Sebastian Zarnekow CLA 2012-07-05 04:56:34 EDT
Available in 2.3
Comment 4 Eclipse Webmaster CLA 2017-10-31 10:48:16 EDT
Requested via bug 522520.

-M.
Comment 5 Eclipse Webmaster CLA 2017-10-31 10:59:16 EDT
Requested via bug 522520.

-M.