当前位置:  开发笔记 > 编程语言 > 正文

在进行清理字符串类型时使用对称相等(使用scala.Proxy)

如何解决《在进行清理字符串类型时使用对称相等(使用scala.Proxy)》经验,为你挑选了0个好方法。

我有一个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("Blah@Blah.com")
val expected = "blah@blah.com"
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

谢谢你的帮助.

推荐阅读
谢谢巷议
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有