我正在尝试执行以下操作:
case class Params(param1: Int, param2: String)
class EntityCreator {
def create(param1: Int, param2: String): Int = {
...
}
def create(params: Params): Int =
// extract a tuple from params and pass it to create
create _ tupled Params.unapply(params).get
}
它给出了以下编译错误:
error: ambiguous reference to overloaded definition,
both method create in class EntityCreator of type (params: Params)Int
and method create in class EntityCreator of type (param1: Int, param2: String)Int
match expected type ?
create _ tupled Params.unapply(params).get
有没有办法告诉Scala编译器我指的是哪两个创建函数?
答案 0 :(得分:0)
当您使用create _
时,编译器无法决定使用哪个创建函数,因为在这种情况下它们都有效。
你必须明确指定像这样的类型信息
((param1: Int, param2: String) => create(param1, param2)) tupled Params.unapply(params).get
此时你可以使用
def create(params: Params): Int =
create(params.param1, params.param2)