在开发DSL时,最简单的方法是限制隐式变量的范围,同时隐藏定义了此类隐式变量的事实?
例如,这是所需的行为...
object External
{
def funNeedingValue(implicit a : String)
{
println(a)
}
}
object Main extends App
{
useValue("Hi") {
// Implicit string "Hi" is only defined in this block
External.funNeedingValue // Prints "Hi"
}
External.funNeedingValue // Compilation error: No implicit String defined
}
以下内容接近,但不具有所有所需的属性...
// The following works, but does not hide the fact that there is an implicit
// variable defined.
object Main extends App
{
{
implicit val implicitValue = "Hi"
External.funNeedingValue // Prints "Hi"
}
External.funNeedingValue // Compilation error: No implicit String defined
}
// The following hides that there is an implicit variable defined, but breaks
// the scoping requirement and destroys thread safety.
abstract class Parent
{
implicit var implicitValue = ""
def useValue(valueToMakeImplicit : String)(f : => Unit)
{
implicitValue = valueToMakeImplicit
f()
}
}
class Child extends Parent
{
def go()
{
useValue("Hi") {
External.funNeedingValue // Prints "Hi"
}
External.funNeedingValue // Scoping issue: also prints "Hi"
}
}
object Main extends App
{
new Child().go()
}
// The following works, but is harder to read and still doesn't really
// hide the implicit value
object Main extends App
{
def useValue(valueToMakeImplicit : String)(f : String => Unit)
{
f(valueToMakeImplicit)
}
useValue("Hi") {
implicit value : String => {
External.funNeedingValue // Prints "Hi"
}
}
External.funNeedingValue // Compilation error: No implicit String defined
}
最佳答案
您可以创建一个宏来进行转换
useValue("Hi") {
// Implicit string "Hi" is only defined in this block
External.funNeedingValue // Prints "Hi"
}
进入
{
implicit val iString: String = "Hi"
External.funNeedingValue
}
我认为没有比上一个没有宏的示例更好的方法了。
关于scala - 隐藏和限定DSL中的隐式变量创建,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23190917/