Skip to content

Commit

Permalink
Return the broadest possible type for the numeric() intrinsic.
Browse files Browse the repository at this point in the history
  • Loading branch information
dweiss committed Aug 1, 2023
1 parent f8ec77e commit ae5aa5a
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,36 @@ public static <T> T add(T op1, T op2) {
}

/**
* Returns a numerical value for the argument for primitive template types. This intrinsic is used
* to apply arithmetic operations on keys. It is invalid for generic types.
* Returns the numerical value for the argument if it is a primitive template type. This intrinsic
* method always returns a {@code double} result for direct calls, but the template preprocessor
* will replace this method invocation with the exact type equal to the template type. So a call
* to:
*
* <pre>
* {@code Intrinsics.<KType>numeric(key)}
* </pre>
*
* with template type {@code KType} equal to {@code int} will return the raw key value (without
* any type conversion):
*
* <pre>
* {@code (key))
* </pre>
*
* <p>This intrinsic is used to apply arithmetic operations on keys. It is invalid for generic
* types.
*/
public static <T> int numeric(T e) {
public static <T> double numeric(T e) {
if (e instanceof Byte
| e instanceof Character
| e instanceof Short
| e instanceof Integer
| e instanceof Float
| e instanceof Long
| e instanceof Double) {
return (int) e;
return (double) e;
}

throw new UnsupportedOperationException("Invalid for generic type: " + e);
}
}

0 comments on commit ae5aa5a

Please sign in to comment.