gpt4 book ai didi

scala - 函数字面量可以在 Scala 中使用类型参数吗?

转载 作者:行者123 更新时间:2023-12-04 18:43:05 25 4
gpt4 key购买 nike

给定以下方法定义 removeAt那是多态的:

def removeAt[T](n: Int, ts: Seq[T]): (Seq[T], T) = ???

我如何声明 removeAt 的列表- 类似的功能?函数字面量可以使用类型参数吗?

如何使以下内容可编译并能够包含 removeAt ?
List[(Int, Seq[_]) => (Seq[_], _)](removeAt)

更新:为什么以下工作正常,所以我可以 foreach通过列表并执行功能?这正是我首先需要的。
val solutions = Seq[(Int, Seq[Any]) => (Seq[Any], Any)](
removeAt
)

最佳答案

博文First-class polymorphic function values in shapeless (1 of 3) — Function values in Scala似乎暗示在“标准”Scala 中没有办法拥有多态函数值:

We can have first-class monomorphic function values and we can have second-class polymorphic methods, but we can't have first-class polymorphic function values ... at least we can't with the standard Scala definitions.



看起来我们需要一个像 shapeless 这样的库.

关于scala - 函数字面量可以在 Scala 中使用类型参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com