简体   繁体   English

别名库隐含在包对象中

[英]Alias library implicits in a package object

Say I have: 说我有:

import org.scalatest.ShouldMatchers._;

This brings a few implicit conversions into scope. 这会将一些implicit转换带入范围。

How can I alias them in a package object so that I can bring the implicits into scope with: 我如何在package object中对它们进行别名,以便我可以将含义带入范围:

import code.ThePackageObject._;

Apparently the ShouldMatchers object extends the ShouldMatchers trait (where the actual definition of the implicits are done). 显然, ShouldMatchers对象扩展了ShouldMatchers特征(完成了implicits的实际定义)。 This is a common idiom that allows to simply mix the trait where you need it. 这是一种常见的习语,可以简单地将特征混合到您需要的地方。 So you can simply mix ShouldMatchers (the trait) in your package object: 所以你可以简单地在你的包对象中混合使用ShouldMatchers (特征):

package object ThePackageObject extends ShouldMatchers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM