Scala 中的函数类型定义和类型擦除

发布于 2024-10-18 11:13:18 字数 447 浏览 1 评论 0原文

给定以下类型和实例:

type operation = (Int, Int) => Int
def add: operation = _ + _

如果我尝试匹配 case 语句中的操作,Scala 会抱怨由于类型擦除而导致未检查的类型:

for (a <- elements) a match {
  case o: operation => // do stuff
}

有没有一种方法可以实现这种基于函数的类型,同时在 case 语句中保持擦除友好?

请注意,这类似于此线程。

Given the following type and instance:

type operation = (Int, Int) => Int
def add: operation = _ + _

If I try to match an operation in a case statement, Scala complains about unchecked typing due to type erasure:

for (a <- elements) a match {
  case o: operation => // do stuff
}

Is there a way to achieve this kind of function-based typing while being erasure-friendly in case statements?

Note, this is similar to this thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-10-25 11:13:18

处理类型擦除的一种简单方法是创建一个非参数化的类。它并不完美,但它有效。使其成为扩展 Function2 的案例类,直接使用或在模式匹配中使用都不会太笨重

scala> case class Operation(f : (Int,Int) => Int) extends ((Int,Int) => Int) {
     | def apply(x : Int, y : Int) = f(x,y)                                   
     | }
defined class Operation

scala> def add = Operation(_ + _)                                             
add: Operation

scala> val y = add(7,3)
y: Int = 10

scala> val elements = List(1, add, 2)
elements: List[Any] = List(1, <function2>, 2)

scala> for (a <- elements) yield a match {        
     | case Operation(f) => f(1,2)        
     | case x : Int => x
     | }
res0: List[Int] = List(1, 3, 2)

。限制是您必须在丢失其类型之前(而不是之后)对操作进行“装箱”。此外,最终每个具体函数类型都会有一个类。

另一种可以说更好的解决方案是不丢失类型信息。使用 Either 保留静态类型信息。

scala> val elements : List[Either[Int, (Int, Int) => Int]] = List(Left(1), Right(_ + _), Left(2))
elements: List[Either[Int,(Int, Int) => Int]] = List(Left(1), Right(<function2>), Left(2))

scala> for (a <- elements) yield a match {
     | case Right(f) => f(1,2)            
     | case Left(x) => x                  
     | }

res1: List[Int] = List(1, 3, 2)

这里的限制是,如果您的列表可以有超过 2 种类型,它就会变得笨拙。但与之前的解决方案不同,它有效地避免了强制 Scala 成为动态类型语言。

One easy way to deal with type erasure is to create an unparamaterized class. It's not perfect, but it works. Make it a case class that extends Function2 and it's not even too clunky to use either directly or in a pattern match

scala> case class Operation(f : (Int,Int) => Int) extends ((Int,Int) => Int) {
     | def apply(x : Int, y : Int) = f(x,y)                                   
     | }
defined class Operation

scala> def add = Operation(_ + _)                                             
add: Operation

scala> val y = add(7,3)
y: Int = 10

scala> val elements = List(1, add, 2)
elements: List[Any] = List(1, <function2>, 2)

scala> for (a <- elements) yield a match {        
     | case Operation(f) => f(1,2)        
     | case x : Int => x
     | }
res0: List[Int] = List(1, 3, 2)

The limitation is that you have to have "boxed" the operation before you lose its type, not after. Also, you end up with one class per concrete function type.

Another, arguably much better, solution is to not lose the type information. Use an Either to retain the static type info.

scala> val elements : List[Either[Int, (Int, Int) => Int]] = List(Left(1), Right(_ + _), Left(2))
elements: List[Either[Int,(Int, Int) => Int]] = List(Left(1), Right(<function2>), Left(2))

scala> for (a <- elements) yield a match {
     | case Right(f) => f(1,2)            
     | case Left(x) => x                  
     | }

res1: List[Int] = List(1, 3, 2)

The limitation here is that it gets clunky if your List can have more than 2 types. But it effectively avoids forcing Scala to be a dynamically typed language, unlike the previous solution.

夏尔 2024-10-25 11:13:18

如果您可以将 a 包装到 Option 中,那么这将起作用:

scala> val a:Option[Any] = Some(add)
a: Option[Any] = Some(<function2>)

scala> a match { case o:Some[operation] => println ("found"); case _ => }
found

If you can wrap a into an Option, then this will work:

scala> val a:Option[Any] = Some(add)
a: Option[Any] = Some(<function2>)

scala> a match { case o:Some[operation] => println ("found"); case _ => }
found
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文