Вот мой Scala код с некоторым примером использованием:Как сделать код более общим
object s {
import scala.reflect.runtime.universe._
def apply[A <: In, B <: In](a: A, b: B) = foo(a, b)
def apply[A <: Int, B <: In](a: A, b: B)(implicit ev: TypeTag[B]) = foo(int(a), b)
def apply[A <: In, B <: Int](a: A, b: B)(implicit ev: TypeTag[A]) = foo(a, int(b))
def apply(a: Int, b: Int) = foo(int(a), int(b))
def apply(a: Int, b: Float) = foo(int(a), float(b))
def apply(a: Int, b: Double) = foo(int(a), double(b))
}
case class int(b: Int) extends In {}
case class float(b: Float) extends In {}
case class double(b: Double) extends In {}
case class foo[A <: In, B <: In](a: A, b: B) extends In {}
def usage = {
val a: struct[int, float] = s(int(1), float(3.1f))
val b: struct[int, struct[int, int]] = s(int(1), s(in
val c: struct[int, int] = s(3, int(5))
val d: struct[int, int] = s(3, 7)
val e: struct[int, double] = s(6, 19.4)
val f: struct[struct[int, int], int] = s(s(1, 1), 9)
// all above work fine
}
как я должен изменить код, чтобы принять любую комбинацию примитивных аргументов без ручного кодирования всех методов перегружать все случаи комбинаций?
Например, так что я могу иметь:
def wanted = {
s(34.4, 1.1)
s(1.3f, 1.1)
s(1.2, 123)
}
т.д.
Ну, почти. До: val c: struct [int, int] = s (3, int (5)) val x: struct [int, struct [int, int]] = s (3, s (3, int (5))) после вашего изменения: val c: struct [In with Product with Serializable, int] = s (3, int (5)) val x: struct [In with Product with Serializable, struct [In with Product with Serializable, int]] = s (3, s (3, int (5))) Теперь он разбивает другую часть моего API – Lambder