有时候,你并不需要指定一个类型是等/子/超于另一个类,你可以通过转换这个类来伪装这种关联关系。一个视界指定一个类型可以被“看作是”另一个类型。这对对象的只读操作是很有用的。
隐函数允许类型自动转换。更确切地说,在隐式函数可以帮助满足类型推断时,它们允许按需的函数应用。例如:
scala> implicit def strToInt(x: String) = x.toInt |
strToInt: (x: String)Int |
scala> "123" |
res0: java.lang.String = 123 |
scala> val y: Int = "123" |
y: Int = 123 |
scala> math.max("123", 111) |
res1: Int = 123 |
<%
指定类型限制,例如:
scala> class Container[A <% Int] { def addIt(x: A) = 123 + x } |
defined class Container |
scala> (new Container[String]).addIt("123") |
res11: Int = 246 |
scala> (new Container[Int]).addIt(123) |
res12: Int = 246 |
scala> (new Container[Float]).addIt(123.2F) |
<console>:8: error: could not find implicit value for evidence parameter of type (Float) => Int |
(new Container[Float]).addIt(123.2) |
^ |
方法可以通过隐含参数执行更复杂的类型限制。例如,List 支持对数字内容执行 sum,但对其他内容却不行。可是 Scala 的数字类型并不都共享一个超类,所以我们不能使用T <: Number
。相反,要使之能工作,Scala 的 math 库对适当的类型 T 定义了一个隐含的 Numeric[T]
。 然后在 List 定义中使用它:
sum[B >: A](implicit num: Numeric[B]): B |
如果你调用List(1,2).sum()
,你并不需要传入一个 num 参数;它是隐式设置的。但如果你调用 List("whoop").sum()
,它会抱怨无法设置 num。
在没有设定陌生的对象为 Numeric 的时候,方法可能会要求某种特定类型的“证据”。这时可以使用以下类型-关系运算符:
scala> class Container[A](value: A) { def addIt(implicit evidence: A =:= Int) = 123 + value } |
defined class Container |
scala> (new Container(123)).addIt |
res11: Int = 246 |
scala> (new Container("123")).addIt |
<console>:10: error: could not find implicit value for parameter evidence: =:=[java.lang.String,Int] |
scala> class Container[A](value: A) { def addIt(implicit evidence: A <%< Int) = 123 + value } |
defined class Container |
scala> (new Container("123")).addIt |
res15: Int = 246 |
在 Scala 标准库中,视图主要用于实现集合的通用函数。例如“min”函数(在 Seq[] 上)就使用了这种技术:
def min[B >: A](implicit cmp: Ordering[B]): A = { |
if (isEmpty) |
throw new UnsupportedOperationException("empty.min") |
reduceLeft((x, y) => if (cmp.lteq(x, y)) x else y) |
} |
其主要优点是:
scala> List(1,2,3,4).min |
res0: Int = 1 |
scala> List(1,2,3,4).min(new Ordering[Int] { def compare(a: Int, b: Int) = b compare a }) |
res3: Int = 4 |
Scala2.8 引入了一种串联和访问隐式参数的快捷方式。
scala> def foo[A](implicit x: Ordered[A]) {} |
foo: [A](implicit x: Ordered[A])Unit |
scala> def foo[A : Ordered] {} |
foo: [A](implicit evidence$1: Ordered[A])Unit |
scala> implicitly[Ordering[Int]] |
res37: Ordering[Int] = scala.math.Ordering$Int$@3a9291cf |
Scala 可以对“更高阶”的类型进行抽象。例如,假设您需要用几种类型的容器处理几种类型的数据。你可能定义了一个 Container 的接口,它可以被实现为几种类型的容器:Option、List 等。你要定义可以使用这些容器里的值的接口,但不想确定值的类型。
这类似与函数柯里化。例如,尽管“一元类型”有类似List[A]
的构造函数,这意味着我们必须满足一个“级别”的类型变量来产生一个具体的类型(就像一个没有柯里化的函数需要只提供一个参数列表来被调用),更高阶的类型需要更多。
scala> trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A } |
scala> val container = new Container[List] { def put[A](x: A) = List(x); def get[A](m: List[A]) = m.head } |
container: java.lang.Object with Container[List] = $anon$1@7c8e3f75 |
scala> container.put("hey") |
res24: List[java.lang.String] = List(hey) |
scala> container.put(123) |
res25: List[Int] = List(123) |
注意:Container是参数化类型的多态(“容器类型”)。
如果我们结合隐式转换 implicits 使用容器,我们会得到“特设的”多态性:即对容器写泛型函数的能力。
scala> trait Container[M[_]] { def put[A](x: A): M[A]; def get[A](m: M[A]): A } |
scala> implicit val listContainer = new Container[List] { def put[A](x: A) = List(x); def get[A](m: List[A]) = m.head } |
scala> implicit val optionContainer = new Container[Some] { def put[A](x: A) = Some(x); def get[A](m: Some[A]) = m.get } |
scala> def tupleize[M[_]: Container, A, B](fst: M[A], snd: M[B]) = { |
| val c = implicitly[Container[M]] |
| c.put(c.get(fst), c.get(snd)) |
| } |
tupleize: [M[_],A,B](fst: M[A],snd: M[B])(implicit evidence$1: Container[M])M[(A, B)] |
scala> tupleize(Some(1), Some(2)) |
res33: Some[(Int, Int)] = Some((1,2)) |
scala> tupleize(List(1), List(2)) |
res34: List[(Int, Int)] = List((1,2)) |
通常有必要来访问一个(泛型)特质的具体子类。例如,想象你有一些泛型特质,但需要可以与它的某一子类进行比较。
trait Container extends Ordered[Container] |
def compare(that: Container): Int |
class MyContainer extends Container { |
def compare(that: MyContainer): Int |
} |
为了调和这一点,我们改用F-
界的多态性。
trait Container[A <: Container[A]] extends Ordered[A] |
奇怪的类型!但可以看到怎样对 A 实现了 Ordered 参数化,它本身就是 Container[A]
所以,现在
class MyContainer extends Container[MyContainer] { |
def compare(that: MyContainer) = 0 |
} |
scala> List(new MyContainer, new MyContainer, new MyContainer) |
res3: List[MyContainer] = List(MyContainer@30f02a6d, MyContainer@67717334, MyContainer@49428ffa) |
scala> List(new MyContainer, new MyContainer, new MyContainer).min |
res4: MyContainer = MyContainer@33dfeb30 |
Container[_]
的子类型,我们可以定义另一个子类并创建 Container[_]
的一个混合列表:
scala> class YourContainer extends Container[YourContainer] { def compare(that: YourContainer) = 0 } |
defined class YourContainer |
scala> List(new MyContainer, new MyContainer, new MyContainer, new YourContainer) |
res2: List[Container[_ >: YourContainer with MyContainer <: Container[_ >: YourContainer with MyContainer <: ScalaObject]]] |
= List(MyContainer@3be5d207, MyContainer@6d3fe849, MyContainer@7eab48a7, YourContainer@1f2f0ce9) |
YourContainer
和 MyContainer
类型确定的下界。这是类型推断的工作。有趣的是,这种类型甚至不需要是有意义的,它只是提供了一个合乎逻辑的最大下界为列表的统一类型。如果现在我们尝试使用 Ordered 会发生什么?
(new MyContainer, new MyContainer, new MyContainer, new YourContainer).min <console>:9: error: could not find implicit value for parameter cmp: Ordering[Container[_ >: YourContainer with MyContainer <: Container[_ >: YourContainer with MyContainer <: ScalaObject]]]
对统一的类型 Ordered[]
不存在了。
Scala 支持结构类型 structural types — 类型需求由接口构造表示,而不是由具体的类型表示。
scala> def foo(x: { def get: Int }) = 123 + x.get |
foo: (x: AnyRef{def get: Int})Int |
scala> foo(new { def get = 10 }) |
res0: Int = 133 |
在特质中,你可以让类型成员保持抽象。
scala> trait Foo { type A; val x: A; def getX: A = x } |
defined trait Foo |
scala> (new Foo { type A = Int; val x = 123 }).getX |
res3: Int = 123 |
scala> (new Foo { type A = String; val x = "hey" }).getX |
res4: java.lang.String = hey |
在做依赖注入等情况下,这往往是一个有用的技巧。
您可以使用 hash 操作符来引用一个抽象类型的变量:
scala> trait Foo[M[_]] { type t[A] = M[A] } |
defined trait Foo |
scala> val x: Foo[List]#t[Int] = List(1) |
x: List[Int] = List(1) |
正如我们所知道的,类型信息在编译的时候会因为擦除而丢失。 Scala 的清单(Manifests)功能,使我们能够选择性地恢复类型信息。清单提供了一个隐含值,根据需要由编译器生成。
scala> class MakeFoo[A](implicit manifest: Manifest[A]) { def make: A = manifest.erasure.newInstance.asInstanceOf[A] } |
scala> (new MakeFoo[String]).make |
res10: String = "" |
参见: https://github.com/twitter/finagle
trait Service[-Req, +Rep] extends (Req => Future[Rep]) |
trait Filter[-ReqIn, +RepOut, +ReqOut, -RepIn] |
extends ((ReqIn, Service[ReqOut, RepIn]) => Future[RepOut]) |
{ |
def andThen[Req2, Rep2](next: Filter[ReqOut, RepIn, Req2, Rep2]) = |
new Filter[ReqIn, RepOut, Req2, Rep2] { |
def apply(request: ReqIn, service: Service[Req2, Rep2]) = { |
Filter.this.apply(request, new Service[ReqOut, RepIn] { |
def apply(request: ReqOut): Future[RepIn] = next(request, service) |
override def release() = service.release() |
override def isAvailable = service.isAvailable |
}) |
} |
} |
def andThen(service: Service[ReqOut, RepIn]) = new Service[ReqIn, RepOut] { |
private[this] val refcounted = new RefcountedService(service) |
def apply(request: ReqIn) = Filter.this.apply(request, refcounted) |
override def release() = refcounted.release() |
override def isAvailable = refcounted.isAvailable |
} |
} |
trait RequestWithCredentials extends Request { |
def credentials: Credentials |
} |
class CredentialsFilter(credentialsParser: CredentialsParser) |
extends Filter[Request, Response, RequestWithCredentials, Response] |
{ |
def apply(request: Request, service: Service[RequestWithCredentials, Response]): Future[Response] = { |
val requestWithCredentials = new RequestWrapper with RequestWithCredentials { |
val underlying = request |
val credentials = credentialsParser(request) getOrElse NullCredentials |
} |
service(requestWithCredentials) |
} |
} |
注意底层服务是如何需要对请求进行身份验证的,而且还是静态验证。因此,过滤器可以被看作是服务转换器。
许多过滤器可以被组合在一起:
val upFilter = |
logTransaction andThen |
handleExceptions andThen |
extractCredentials andThen |
homeUser andThen |
authenticate andThen |
route |