score:94
It's easy to forget that a trait may extend a class. If you use a trait, you can postpone the decision of which constructor to call, like this:
trait Extended extends Base {
...
}
object Extended {
def apply(arg1: Int) = new Base(arg1) with Extended
def apply(arg2: String) = new Base(arg2) with Extended
def apply(arg3: Double) = new Base(arg3) with Extended
}
Traits may not themselves have constructor parameters, but you can work around that by using abstract members instead.
score:2
I would pick the most generic one (in this case, String) and do the internal conversion yourself if it meets the other criteria.
Although I admit this is not the best solution and something strikes me as wrong about it. :-(
score:2
This is a silly answer that would probably work somewhat but might be too much effort if the Java class has way too many constructors, but:
Write a subclass in Java that implements a constructor that takes all the inputs the various other constructors would and calls the proper constructor of its superclass based on the presence or absence of inputs (via usage of "null" or some sort of sentinel values), then subclass that Java class in Scala and assign the sentinel values as default parameters.
score:5
EDIT - this is from a question on the scala mailing list which I thought was duplicated here. My answer relates to providing three different constructors (i.e. replicating the Java design), and not extending the class
Assuming that each of your constructors ultimately create the state S
of the object, create a companion object with "static" methods to create this state
object Base {
private def stateFrom(d : Double) : S = error("TODO")
private def stateFrom(s : Str) : S = error("TODO")
private def stateFrom(i : Int) : S = error("TODO")
}
Then create a private constructor taking the state and (public) overloaded constructors which defer to the primary constructor
import Base._
class Base private(s : S) { //private constructor takes the state
def this(d : Double) = this(stateFrom(d))
def this(str : String) = this(stateFrom(str))
def this(i : Int) = this(stateFrom(i))
//etc
}
Source: stackoverflow.com
Related Query
- In Scala, how can I subclass a Java class with multiple constructors?
- How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion
- How to implement Java interface in Scala with multiple variable parameter methods (type eraser issue)?
- How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?
- How can i compile java record with scala code?
- How can I replace a java mutable class with its immutable couterpart in scala?
- Scala: subclass a Java class with multiple constructors?
- How Nothing can be subclass of all types when multiple inheritance is not supported in scala
- How can I call Scala function with class name and function name as strings
- how can i override functions ( create, delete, modify, etc ) of crudify class ( scala with lift )
- How to make my java class writable by extending it with scala class?
- Scala: how to implement Java class with more constructors
- How can I match constructor types with Java reflection with Scala primitive types?
- How can I create multiple Datasets with different class types from one general Dataset?
- How to join multiple RDD having Object using scala with case class
- How I can stub abstract java class with protected abstract methods via ScalaMock?
- Java to Scala with multiple constructors
- how to create scala class similar to java bean but with simple logic to modify the input
- How can one "associate" test scripts with a Class in Scala
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- How can I convert a Java Iterable to a Scala Iterable?
- Can a Scala class extend multiple classes?
- How can Scala receive multiple parameters in a method definition?
- Spark: How to map Python with Scala or Java User Defined Functions?
- How can one provide manually specialized implementations with Scala specialization?
- How to pattern match a class with multiple argument lists?
- Scala - can yield be used multiple times with a for loop?
- In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?
- Can I use a scala class which implements a java interface from Java?
- How to have SBT subproject with multiple Scala versions?
More Query from same tag
- Get the vertex with highest degree of a large graph in GraphX
- How to do escaping idiomatically in Scala
- Loading JSON data in mongodb using sparksql
- Scala akka ActorFor path
- Collection and toArray in Scala
- start/stop lifecycle of application with sbt-native-packager
- ToolBox Import Error
- How to read this flatMap code?
- Spark collect()/count() never finishes while show() runs fast
- why can't "$$" be used as a delimitter in splitting scala string to array?
- Eliminate duplicates, change label with scala.xml.transform.RuleTransformer
- Why does cons function called explicity works over Int in Scala?
- Filtering a Scala Set from Java
- Weird negative length array created into Class.getDeclaredFields that makes JVM to throw a NegativeArraySizeException
- Multi-line package declaration in Scala
- IntelliJ Unable to Compile Scala Slick CaseClassShape
- writing a custom get method for a scala map
- Alias library implicits in a package object
- How to get current project name in sbt build.scala
- Using regex to access values from a map in keys
- how to convert a list of string to case class?
- Creating and Querying a Volatile table using Teradata JDBC in Spark using Scala
- What is the best way to get the last inserted auto-generated ID using slick?
- Why this scala code has a compilation error type mismatch
- scala's type checker doesn't recognize types in abstract path-dependent classes scenario
- Scala function that returns same function
- Scala recursive function returning Either
- cats-effect:How to transform Map[x,IO[y]] to IO[Map[x,y]]
- Best way to handle false unused imports in intellij
- How to override a implicit function of companion object?