将隐式methods/fields引入匿名函数

Introduce implicit methods/fields into anonymous function

我刚刚开始使用 Scala,但在尝试创建一个特定对象时遇到了困难。

我有一个对象 IdentifierFactory,我想用它来生成 Identifier 实例。我是这样定义的:

object IdentifierFactory {
    def apply(f: => Any):Identifier = {
        f; validate

        Identifier(...)
    }

    def validate:Unit = ???
}

我希望能够按以下方式使用它:

IdentifierFactory {
    setName("name");
    addResource(resource);
}

如何声明我的 setNameaddResource(或者我可以访问的 var)以便它们仅在匿名函数中可见?

您可以将构建器传递给您的匿名函数:

IdentifierFactory { b =>
    b.setName("name")
    b.addResource(null)
}

object IdentifierFactory {
    trait Builder {
       def setName(name: String)
       def addResource(r: Any)
    }

    case class Identifier(name: String, rs: List[Any])

    def apply(f: Builder => Any):Identifier = {
        var nm = "default"
        var rs = List[Any]()
        f(new Builder {
           def setName(name: String) = nm = name 
           def addResource(r: Any) = rs ::= r
        })
        validate

        Identifier(nm, rs)
    }

    def validate: Unit = {}
}

scala> IdentifierFactory { b =>
 |         b.setName("name")
 |         b.addResource(null)
 |     }
res4: IdentifierFactory.Identifier = Identifier(name,List(null))

顺便说一下,您仍然可以通过将名称和资源列表传递给具有默认值的函数来执行相同的操作:

object IdentifierFactory {
    case class Identifier(name: String, rs: List[Any])

    def apply(nm: String = "default", rs: List[Any] = List[Any]()): Identifier = {          
        //validate it right here    
        Identifier(nm, rs)
    }

}

scala> IdentifierFactory()
res7: IdentifierFactory.Identifier = Identifier(default,List())

scala> IdentifierFactory(rs = List(null))
res8: IdentifierFactory.Identifier = Identifier(default,List(null))

scala> IdentifierFactory("nm", List(null))
res9: IdentifierFactory.Identifier = Identifier(nm,List(null))

scala> IdentifierFactory("nm")
res10: IdentifierFactory.Identifier = Identifier(nm,List())

我还建议返回 OptionEither 而不是验证异常。