是否存在 unapply 总是返回 Option 的规则?
我尝试创建一个用于模式匹配的 unapply
方法,并尝试使其返回与 Option
不同的内容,但是,Eclipse 将其显示为错误。是否存在 unapply
必须返回 Option[T]
的规则?
编辑:这是我尝试使用的代码。我切换了上一节中的代码,以便 unapply
返回一个布尔值,
import java.util.regex._
object NumberMatcher {
def apply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
def unapply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
}
object x {
def main(args : Array[String]) : Unit = {
val strings = List("geo12","neo493","leo")
for(val str <- strings) {
str match {
case NumberMatcher(group) => println(group)
case _ => println ("no")
}
}
}
}
Eclipse 表示对象 NumberMatcher 的参数数量错误
。这是为什么?
I tried to create an unapply
method to use in pattern matching, and I tried to make it return something different than Option
, however, Eclipse shows that as an error. Is it a rule that unapply
must return an Option[T]
?
EDIT: here's the code I'm trying to use. I switched the code from the previous section so that unapply
returns a Boolean
import java.util.regex._
object NumberMatcher {
def apply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
def unapply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
}
object x {
def main(args : Array[String]) : Unit = {
val strings = List("geo12","neo493","leo")
for(val str <- strings) {
str match {
case NumberMatcher(group) => println(group)
case _ => println ("no")
}
}
}
}
Eclipse says wrong number of arguments for object NumberMatcher
. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想使用
unapply
返回某些内容,请在Some
中返回它。返回Boolean
只是测试是否可以匹配。以下是模式匹配的翻译方式:
假设
NumberMatcher
返回Option[...]
,它将执行以下操作:如果
NumberMatcher
返回 Boolean,则你可以让它收到一些东西。在这种情况下,会发生以下情况:变为
请注意,这是一个非常肤浅的解释。大小写匹配可以测试常量、有额外的保护条件、替代方案、递归使用
unapply
、使用unapplySeq
等。这里我只展示非常基本的用法来解决特定的问题问题。我强烈建议寻找有关模式匹配的更完整的解释。If you want to return something with
unapply
, return it insideSome
. ReturningBoolean
just tests if the match can be made or not.Here is how a pattern matching is translated:
Assuming
NumberMatcher
returnsOption[...]
, it will do:If
NumberMatcher
returns Boolean, then you can have it receive something. In that case, this is what happens:becomes
Note that this is a very superficial explanation. Case matches can test for constants, have additional guard conditions, alternatives, use
unapply
recursively, useunapplySeq
, etc. Here I'm only showing very basic usage to address a specific question. I strongly advise searching for a fuller explanation of pattern matching.再次查看这个示例。
我引用
Take a look at this example again.
I quote
当您定义
unapply
以返回Boolean
时,您表明该模式没有任何要匹配(或绑定)的通配符。所以这个解提取器的 case 语句应该是case NumberMatcher =>; println(str)
,并给它一个变量来填充是错误的。或者,使
case NumberMatcher(group) => println(group)
您需要定义unapply()
以返回Option[String]
When you defined
unapply
to return aBoolean
, you were indicating that the pattern doesn't have any wildcards to match (or bind). So the case statement for this unextractor should becase NumberMatcher => println(str)
, and giving it a variable to fill is wrong.Alternatively, to make
case NumberMatcher(group) => println(group)
you need to defineunapply()
to returnOption[String]
我们也可以用上面的代码来实现
we can achieve this with above code as well