我不太确定你想在这里实现什么,如果我理解正确的话你想避免打字(a:Int, b:Int, x:Double, y:Double, name:String)
两次。
定义怎么样FType
你自己预先,然后简单地重用它f
and callF
?
object Foo {
type FType = (Int,Int,Double,Double,String) => Unit
def f: FType = (a, b, x, y, name) => ()
def callF(func: FType) = func(0,0,0,0,"")
}
如果你真的想抽象FType
,这是一个明显不同的问题,但情况似乎并非如此,因为您通过调用来强制类型func(0,0,0,0,"")
.
你没有decltype
在 Scala 中,因为类型不像在 Idris 中那样不是一等公民。也就是说,你应该能够使用它来编写它无形的 https://github.com/milessabin/shapeless和/或宏。
如果您想修复类型和参数并重用它们,最简单的解决方案是将它们变成case class
。然后你可以使用import
直接访问您的字段:
object Foo {
case class FArgs(a: Int, b: Int, x: Double, y: Double, name: String)
def f(args: FArgs): Unit = {
import args._
println(name) // or whatever you want to do
}
def callF(func: FArgs => Unit) = func(FArgs(0,0,0,0,""))
}