Operators for operations with java.lang.Number [message #1805077] |
Fri, 05 April 2019 08:36  |
Eclipse User |
|
|
|
Hello,
I have a question about the operators on standard types (plus, minus, etc.) taking java.lang.Number as parameter.
In DoubleExtensions, there are functions like this:
@Pure
public static double operator_plus(Double a, Number b) {
return a + b.doubleValue();
}
It allows to use the plus operator to add a Double to a Number.
This function doesn't have an equivalent for Float, Integer, and the others but I'd like something like this in FloatExtensions:
@Pure
public static double operator_plus(Float a, Number b) {
return a + b.doubleValue();
}
I tried to implement it myself in the DSL I'm working on, but when I do it, it fails when I try to use the + operator between a Float and a Number. It seems it's not able to use the good operator_plus I defined, as it tries to use another one (which won't work). However, if I use something else than Number (like AtomicDouble), the operator_plus I define works fine.
To be more precise when I have this (in an Xtend based language - SARL):
val f : Float = 0.0f
val l : Number = 0.0f
val res = f - l
I get this error:
Type mismatch: cannot convert from Number to byte for the l. Using a Double instead of a Float would work fine, but I want to be able to use operator_plus with a Float and a Number.
So my questions are, is there a reason for the operator_plus to not be defined between Float and Number in FloatExtensions? And, do you have any idea why it's trying to use another definition of the operator_plus? Is there something I should do to allow it to work?
[Updated on: Fri, 05 April 2019 08:38] by Moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03357 seconds