Operator == with null operand [message #1795053] |
Thu, 13 September 2018 11:53  |
Eclipse User |
|
|
|
Dear all.
I have defined several extension methods (which are implicitly imported):
def boolean operator_equals(AtomicLong left, int right) {}
def boolean operator_equals(AtomicLong left, short right) {}
def boolean operator_equals(AtomicLong left, long right) {}
def boolean operator_equals(AtomicLong left, float right) {}
def boolean operator_equals(AtomicLong left, double right) {}
def boolean operator_equals(AtomicLong left, byte right) {}
def boolean operator_equals(AtomicLong left, Number right) {}
def boolean operator_equals(AtomicInteger left, int right) {}
def boolean operator_equals(AtomicInteger left, short right) {}
def boolean operator_equals(AtomicInteger left, long right) {}
def boolean operator_equals(AtomicInteger left, float right) {}
def boolean operator_equals(AtomicInteger left, double right) {}
def boolean operator_equals(AtomicInteger left, byte right) {}
def boolean operator_equals(AtomicInteger left, Number right) {}
The following line is compiled without error:
def void fct(AtomicInteger value) {
if (value == null) {
}
}
But the following line causes a compilation error (ambiguous feature call):
def void fct(AtomicInteger value) {
if (null == value) {
}
}
As you could see, the operands of the == operator are switched.
Semantically, both definitions are equivalent.
My question is: what is the best way to support the failing case?
Is possible to define an operator_equals function and specifying that this function is usable when the left argument is null?
Should I define operator_equals(Object left, Number right)? In this case, the type compiler of Xbase hould be fixed to avoid the ambiguous feature call error.
What do you think?
All the best.
Stéphane.
|
|
|
|
|
|
Re: Operator == with null operand [message #1795060 is a reply to message #1795059] |
Thu, 13 September 2018 14:53  |
Eclipse User |
|
|
|
Hi
IIRC The Xtend ==/!= semantics evolved requiring use of ===/!== to avoid warnings on null tests.
Unless you have weird semantics that require null to go through your custom routines, why not just use ===?
Regards
Ed Willink
|
|
|
Powered by
FUDForum. Page generated in 0.08014 seconds