scala.js - Overload private constructor with polymorphic arguments in Scala -
i'm curious best solution in scala:
class myclass private (x: any, y: int) { def this(x: int, y: int) = this(x, y) def this(x: string, y: int) = this(x, y) } val x0 = new myclass(1, 1) val x1 = new myclass("1", 1) //val x2 = new myclass(1.0, 1) // correctly doesn't typecheck
the error below doesn't make lot of sense me, because appears viable constructor defined before auxiliary constructor:
error:(3, 31) called constructor's definition must precede calling constructor's definition def this(x: int, y: int) = this(x, y) ^
for more context, i'm trying deal javascript apis in scala.js functions take parameter can either string
or js.object
, think exemplifies issue.
ascribing type any
explicitly help:
class myclass private (x: any, y: int) { def this(x: int, y: int) = this(x: any, y) def this(x: string, y: int) = this(x: any, y) }
in case, constructors call recursively, nonsensical.
Comments
Post a Comment