-1

Is it possible to define custom autoboxing in Java?

I need to automatically convert java.lang.Number into my class org.expr.NumberExpression when typing parameters of a function. For two parameters, I ended up writing four very similar methods:

public class Assemble {

  public NumberExpression add(NumberExpression a, NumberExpression b) {
    // do something
  }

  public NumberExpression add(NumberExpression a, Number b) {
    return add(a, new NumberConstant(b));
  }

  public NumberExpression add(Number a, Number b) {
    return add(new NumberConstant(a), new NumberConstant(b));
  }

  public NumberExpression add(Number a, NumberExpression b) {
    return add(new NumberConstant(a), b);
  }

}

So I can type:

assemble.add(5, assemble.add(7, 3));
assemble.add(5, assemble.add(7, 3), 8, 15); // does not work

However, I think this becomes unmanageable for 10 parameters (that I wanted to do); I guess I would need to write 1024 similar methods.

The other solution I was thinking about is to write a method like:

  public NumberExpression add(NumberExpression... numbers) {
    // do something
  }

But would I be able to type Number and NumberExpression mixed together as parameters?

Joe DiNottra
  • 836
  • 8
  • 26
  • 2
    No, that is not possible. Autoboxing/unboxing is a language feature, it cannot be customized. – MC Emperor Oct 24 '20 at 18:12
  • Most likely you'll need to create a converter in your `NumberExpression` class that takes a `Number` and creates a `NumberExpression`. – Michael Welch Oct 24 '20 at 18:12
  • Or you need some kind of preprocessor. – MC Emperor Oct 24 '20 at 18:13
  • 4
    Does this answer your question? [Is autoboxing possible for the classes I create?](https://stackoverflow.com/questions/17619724/is-autoboxing-possible-for-the-classes-i-create) – flaxel Oct 24 '20 at 18:13
  • "No, that is not possible" -- Sigh. – Joe DiNottra Oct 24 '20 at 18:13
  • The cleanest workaround is probably to define a static method that creates a new `NumberConstant` and do an `import static` so you can write something like `const(8)` instead of `new NumberConstant(8)`. – kaya3 Oct 24 '20 at 18:27
  • Mixed param for vararg is not possible, you can create a static method to return `NumberExpression` from Number and pass as parameter. – Eklavya Oct 24 '20 at 18:27
  • Can't you have your NumberExpression extend Number and declare `NumberExpreassion add(Number... numbers)` – Turo Oct 24 '20 at 18:36
  • @Turo "Can't you have your NumberExpression extend Number..." -- Didn't think of that. I'll think about this. – Joe DiNottra Oct 24 '20 at 19:01
  • @Turo I realized I cannot extend Number, since the NumberExpression already extends ScalarExpression, that in turn extends Expression. – Joe DiNottra Oct 24 '20 at 20:34
  • Last Idea: Composition over Inheritance, can't your NumberExpresion only have a ScalarExpression, but I suppose that would bring other trouble... – Turo Oct 24 '20 at 21:50

2 Answers2

2
  1. As people said it can't be done
  2. How your problem usually solved is you wrap the values yourself and have only one method (look at BigDecimal)
assemble.add(
    new IntegerConstant(5),
    assemble.add(new IntegerConstant(7), new IntegerConstant(3))

or using a factory:

assemble.add(
    NumberExpression.of(5),
    assemble.add(NumberExpression.of(7), NumberExpression.of(3))

where:

   class NumberExpression {
       public static IntegerConstant of(Integer v) {
           //...
       }

       public static DoubleConstant of(Double v) {
           //...
       }
    }
heldev
  • 846
  • 7
  • 9
0

No, you can't define custom autoboxing

Juan C Nuno
  • 476
  • 1
  • 3
  • 8