[英]Is it possible to use implicit conversions for parameters to extractors (unapply) in Scala?
我创建了一个名为CaseInsensitive的类,它包装了一个字符串(请参阅实现在Scala中执行不区分大小写的比较的字符串类 )。
我创建了一个case类,它有一个CaseInsensitive类型的成员变量,所以它得到一个默认的unapply方法,它提取一个CaseInsensitive类型的变量,但我希望像这样使用它:
case class PropertyKey( val name : CaseInsensitive )
val foo = new PropertyKey("foo")
val result = foo match {
case PropertyKey("foo") => true
case _ => false
}
此代码无法编译:(在提取器行上,而不是构造函数行)
type mismatch;
found : java.lang.String("foo")
required: com.acme.CaseInsensitive
但是我认为我从String到CaseInsensitive的隐式转换会使它能够编译,而不是我必须输入更详细的:
case class PropertyKey( val name : CaseInsensitive )
val foo = new PropertyKey("foo")
val result = foo match {
case PropertyKey(CaseInsensitive("foo")) => true
case _ => false
}
以下是CaseInsensitive的实现:
/** Used to enable us to easily index objects by string, case insensitive
*
* Note: this class preserve the case of your string!
*/
case class CaseInsensitive ( val _s : String ) extends Proxy {
require( _s != null)
val self = _s.toLowerCase
override def toString = _s
def i = this // convenience implicit conversion
}
object CaseInsensitive {
implicit def CaseInsensitive2String(c : CaseInsensitive) = if ( c == null ) null else c._s
implicit def StringToCaseInsensitive(s : String) = CaseInsensitive(s)
def fromString( s : String ) = s match {
case null => None
case _ => Some(CaseInsensitive(s))
}
}
您可以随时定义一个方便提取器并导入它(随意使用较短的名称):
object PropertyKeyCI {
def unapply(p: PropertyKey): Option[String] = Some(p.name.self)
}
然后,提取是:
val foo = new PropertyKey("foo")
val result = foo match {
case PropertyKeyCI("foo") => true
case _ => false
}
虽然请注意,这与PropertyKeyCI(“Foo”)匹配为false,因为您的“CaseInsensitive”类实际上是“LowerCase”类。 我这样说是因为我很难想象unapply()方法会有什么样的行为。 从您的case类默认情况下,您将返回原始(unlowercased)字符串的Option [String],这会给出这种不直观的行为:
// result == false !!!!
val foo = new CaseInsensitive("Foo")
val result = foo match {
case CaseInsensitive("foo") => true
case _ => false
}
val CaseInsensitive(s) = "aBcDeF"
assertFalse(s == "abcdef")
Awwww ....折腾它。 只需使用DOS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.