如何使用特征scala.Proxy [英] How do I use the trait scala.Proxy

查看:250
本文介绍了如何使用特征scala.Proxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 API 中找到它,并希望查看一个或两个示例以及一个解释它是有益的。

I just found it in the API and would like to see one or two examples along with an explanation what it is good for.

推荐答案

Proxy trait为创建提供了有用的依据代表,但请注意,它仅提供任何等于 hashCode中的方法的实现 toString )。您必须自己实现任何其他转发方法。 Proxy通常与 pimp-my-library模式一起使用:

The Proxy trait provides a useful basis for creating delegates, but note that it only provides implementations of the methods in Any (equals, hashCode, and toString). You will have to implement any additional forwarding methods yourself. Proxy is often used with the pimp-my-library pattern:

class RichFoo(val self: Foo) extends Proxy {
   def newMethod = "do something cool"
}

object RichFoo {
   def apply(foo: Foo) = new RichFoo(foo)
   implicit def foo2richFoo(foo: Foo): RichFoo = RichFoo(foo)
   implicit def richFoo2foo(richFoo: RichFoo): Foo = richFoo.self
}

标准库还包含一组可用于创建收集代理的特征( SeqProxy SetProxy MapProxy 等)。

The standard library also contains a set of traits that are useful for creating collection proxies (SeqProxy, SetProxy, MapProxy, etc).

最后,scala-incubator中有一个编译器插件( AutoProxy插件)。另请参见此问题

Finally, there is a compiler plugin in the scala-incubator (the AutoProxy plugin) that will automatically implement forwarding methods. See also this question.

这篇关于如何使用特征scala.Proxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆