Scala 如何绑定数据对象
我有一个数据对象:Scala 如何绑定数据对象,scala,Scala,我有一个数据对象: case class Id(val value: String) extends AnyVal { def bind[A](f: ((String) => A)): A = { f(value) } } 我想将Id绑定到字符串的第一个参数。格式: id.bind(template.format) 但是我得到了错误类型不匹配:Seq[Any]=>String=>String 我相信这是因为template.format可以接受不同数量的参数 有没有一种
case class Id(val value: String) extends AnyVal {
def bind[A](f: ((String) => A)): A = {
f(value)
}
}
我想将Id绑定到字符串的第一个参数。格式
:
id.bind(template.format)
但是我得到了错误类型不匹配:Seq[Any]=>String=>String
我相信这是因为template.format
可以接受不同数量的参数
有没有一种方法可以让我制作出这样一个好的可重用绑定函数
编辑:(我不想泄露Id的val,因为我正在尝试实施“告诉-不要问”策略)如果在绑定调用中使用lambda,那么一切都应该正常:
id.bind(s => template.format(s))
或者,您可以将绑定函数更改为接受Seq[Any]
:
def bind[A](f: ((Seq[Any]) => A)): A = {
f(Seq(value))
}
或:
def bind[A](f: (String*) => A): A = {
f(value)
}
id.bind(template.format) // Works!!!