首页 > 解决方案 > 从 Scala API 中消除身份包装器类型

问题描述

假设我正在尝试“抽象执行”:

import scala.language.higherKinds

class Operator[W[_]]( f : Int => W[Int] ) {
  def operate( i : Int ) : W[Int] = f(i)
}

现在我可以定义一个Operator[Future]Operator[Task]等等。例如......

import scala.concurrent.{ExecutionContext,Future}
def futureSquared( i : Int ) = Future( i * i )( ExecutionContext.global )

在 REPL 风格中...

scala> val fop = new Operator( futureSquared )
fop: Operator[scala.concurrent.Future] = Operator@105c54cb

scala> fop.operate(4)
res0: scala.concurrent.Future[Int] = Future(<not completed>)

scala> res0
res1: scala.concurrent.Future[Int] = Future(Success(16))

万岁!

但我也可能想要一个简单的同步版本,所以我在某个地方定义

type Identity[T] = T

我可以定义一个同步运算符...

scala> def square( i : Int ) : Identity[Int] = i * i
square: (i: Int)Identity[Int]

scala> val sop = new Operator( square )
sop: Operator[Identity] = Operator@18f2960b

scala> sop.operate(9)
res2: Identity[Int] = 81

甜的。

但是,结果的推断类型是Identity[Int],而不是更简单、直接的 ,这很尴尬Int。当然,这两种类型实际上是相同的,因此在各方面都相同。但我希望我的图书馆的客户对这种抽象过度执行的东西一无所知,不要混淆。

我可以手写一个包装...

class SimpleOperator( inner : Operator[Identity] ) extends Operator[Identity]( inner.operate ) {
  override def operate( i : Int ) : Int = super.operate(i)
}

这确实有效...

scala> val simple = new SimpleOperator( sop )
simple: SimpleOperator = SimpleOperator@345c744e

scala> simple.operate(7)
res3: Int = 49

但这感觉很像样板,特别是如果我的抽象过度执行类有很多方法,而不仅仅是一个。而且我必须记住随着泛型类的发展保持包装器同步。

是否有一些更通用、可维护的方法来获得一个版本Operator[Identity],使包含类型从类型推断和 API 文档中消失?

标签: scala

解决方案


这更多的是长评论而不是答案......

但是,结果的推断类型是 Identity[Int],而不是更简单、直接的 Int,这很尴尬。当然,这两种表观类型实际上是相同的,因此在各方面都是相同的。但我希望我的图书馆的客户对这种抽象过度执行的东西一无所知,不要混淆。

这听起来像你想转换Indentity[T]T......你考虑过类型归属吗?

scala>def f[T](t: T): Identity[T] = t

scala>f(3)
// res11: Identity[Int] = 3

scala>f(3): Int
// res12: Int = 3

// So in your case
scala>sop.operate(9): Int
// res18: Int = 81

推荐阅读