我有一个scala(2.10.4)应用程序,其中大量传递了电子邮件地址,并且我想实现一个在IO处调用的抽象方法,以“清理”已验证的电子邮件地址。
我几乎想要使用scala.Proxy
,但是我遇到了不对称相等的问题。
class SanitizedEmailAddress(s: String) extends Proxy with Ordered[SanitizedEmailAddress] {
val self: String = s.toLowerCase.trim
def compare(that: SanitizedEmailAddress) = self compareTo that.self
}
object SanitizedEmailAddress {
def apply(s: String) = new SanitizedEmailAddress(s)
implicit def sanitize(s: String): SanitizedEmailAddress = new SanitizedEmailAddress(s)
implicit def underlying(e: SanitizedEmailAddress): String = e.self
}
我想要
val sanitizedEmail = SanitizedEmailAddress("[email protected]")
val expected = "[email protected]"
assert(sanitizedEmail == expected) // => true
assert(expected == sanitizedEmail) // => true, but this currently returns false :(
或具有类似功能的东西。有没有简便的方法可以做到这一点?
assert(sanitizedEmail.self == expected) // => true (but pretty bad, and someone will forget)
// can have a custom equality method and use the "pimp-my-lib" pattern on strings, but then we have to remember to use that method every time
谢谢你的帮助。
最佳答案
对不起,我认为这是不可能的。
我不确定是否也想要这样。如果String
确实等于SanitizedEmailAddress
,那么SanitizedEmailAddress
包装器实际上表示什么?
我认为String
与SanitizedEmailAddress
不具有可比性,并且要求用户在比较输入之前先对输入进行“ sanitizer ”会更加一致。