0

I need to have a Scala class HugeDecimal that inherit from java.math.BigDecimal. It cannot be a trait for internal reasons. The following simple implementation:

class HugeDecimal extends java.math.BigDecimal {
}

Raises this error:

Error:(1187, 37) overloaded method constructor BigDecimal with alternatives:
  (x$1: Long,x$2: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: Long)java.math.BigDecimal <and>
  (x$1: Int,x$2: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: Int)java.math.BigDecimal <and>
  (x$1: java.math.BigInteger,x$2: Int,x$3: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: java.math.BigInteger,x$2: Int)java.math.BigDecimal <and>
  (x$1: java.math.BigInteger,x$2: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: java.math.BigInteger)java.math.BigDecimal <and>
  (x$1: Double,x$2: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: Double)java.math.BigDecimal <and>
  (x$1: String,x$2: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: String)java.math.BigDecimal <and>
  (x$1: Array[Char],x$2: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: Array[Char])java.math.BigDecimal <and>
  (x$1: Array[Char],x$2: Int,x$3: Int,x$4: java.math.MathContext)java.math.BigDecimal <and>
  (x$1: Array[Char],x$2: Int,x$3: Int)java.math.BigDecimal
 cannot be applied to ()

I know I can do:

class HugeDecimal(d: Double) extends java.math.BigDecimal(d) {
  def this(str: String) = this(str.toDouble)
  def this(i: Int) = this(i.toDouble)
}

But I need to be able to inherit all the constructors from the superclass without favoring any single superclass constructor. I.e., I need to the String constructor call the superclass' String constructor.

The answers Scala inheritance from Java class: select which super constructor to call and In Scala, how can I subclass a Java class with multiple constructors? suggest using traits or a primary constructor that is delegated to by auxiliary constructors, but neither of these works for my scenario, since I need to be accessible from Java code that can call things like:

new HugeDecimal("12.34")
new HugeDecimal(1234)

I there any solution, or do I need to implement this class in Java?

marcprux
  • 9,845
  • 3
  • 55
  • 72

1 Answers1

2

You cannot inherit constructors. It does not matter if you implement it in java or in scala, if you want to have several constructors, you will have to implement each one of them.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • The problem isn’t not being able to inherit the constructor. The problem is that I need to be able to choose which superclass constructor to which to delegate. – marcprux Jan 07 '18 at 20:07
  • And why aren't you able to choose that? – Dima Jan 07 '18 at 21:29
  • The String constructor needs to delegate to the superclass' String constructor, and the Int constructor needs to delegate to the superclass' int constructor. Is that possible? If so, I'd love to see an example. – marcprux Jan 07 '18 at 23:04
  • Ok, I see what you mean now ... Well, you could call something like this from java code: `HugeDecimal.apply("12.34")` (this works if `HugeDecimal` is an object and there is no "companion" class ... Otherwise, you'd have to do `HugeDecimal$.MODULE$.apply("12.34")` ). Then you could use one of the methods described in the answers you quoted - basically, use traits to extend, and object `apply` methods to invoke correct constructor. – Dima Jan 08 '18 at 02:11