This may help you:
import scala.reflect._ abstract class MyClassBuilder[A <: MyClass: ClassTag] { def apply() = classTag[A].runtimeClass.newInstance.asInstanceOf[A] }
You may find that I did not pass (String, String) * here, since they are redundant to instantiate. Anyway, you have access to the whole instance of A here, so at least you can change the param after creating the instance. This is not exactly what you want - as you seem to be looking for a way to extend SomeOtherClass at runtime. However, creating a new type in scala is not possible, but you might think of it as a prototype - just take an instance of SomeOtherClass and mutate once inside apply() :
import scala.reflect._ object Ctx { class MyClass { private[Ctx] var _param = Map.empty[String, String] def param = _param } abstract class MyClassBuilder[A <: MyClass: ClassTag] { def apply(p: (String, String) *) = { val i = classTag[A].runtimeClass.newInstance.asInstanceOf[A] i._param = Map(p: _*) i } } } scala> class MySpecialClass extends Ctx.MyClass defined class MySpecialClass scala> object MySpecialBuilder extends Ctx.MyClassBuilder[MySpecialClass] defined module MySpecialBuilder scala> MySpecialBuilder("A" -> "b") res12: MySpecialClass = MySpecialClass@2871ed4a scala> res12.param res13: scala.collection.immutable.Map[String,String] = Map(A -> b)
Otherwise, you will have to deal with reflection in compilation time.
An interesting alternative is extensible records (Shapeless2) - they practically allow you to create a new type in parts, but you will have to solve with HList instead of a class:
import shapeless._ ; import syntax.singleton._ ; import record._ import shapeless.ops.record.Updater import scala.reflect.runtime.universe._ val param = Witness("param") val someFunW = Witness("someFun") //defining classess (instead of "class MyClass") type Param = Map[String, String] with KeyTag[param.T, Map[String, String]] type SomeFun = (String => String) with KeyTag[someFunW.T, (String => String)] type MyClass = Param :: HNil type MySpecialClass = SomeFun :: MyClass def someFun(s: String) = s + "A" //defining default values (instead of "val param = ...") def newMyClass[T <: HList : TypeTag]: T = ( if (typeTag[T] == typeTag[MyClass]) (param ->> Map.empty[String, String]) :: HNil else if (typeTag[T] == typeTag[MySpecialClass]) (someFunW ->> someFun _) :: newMyClass[MyClass] else HNil ).asInstanceOf[T] //Defining builder def buildWithParam[T <: HList: TypeTag](p: Map[String, String])(implicit ev: Updater[T, Param]) = newMyClass[T] + ("param" ->> p) scala> buildWithParam[MySpecialClass](Map("a" -> "v")) res6: ... = <function1> :: Map(a -> v) :: HNil scala> res6("someFun").apply("a") res7: String = aA scala> buildWithParam[MyClass](Map("a" -> "v")) res8: ... = Map(a -> v) :: HNil
source share