是否存在 unapply 总是返回 Option 的规则?

发布于 2024-08-14 11:40:22 字数 900 浏览 1 评论 0原文

我尝试创建一个用于模式匹配的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜柠檬 2024-08-21 11:40:22

如果您想使用 unapply 返回某些内容,请在 Some 中返回它。返回 Boolean 只是测试是否可以匹配。

以下是模式匹配的翻译方式:

str match { 
  case NumberMatcher(group) => println(group)
  case _ => println("no")
}

假设 NumberMatcher 返回 Option[...],它将执行以下操作:

val r1 = NumberMatcher.unapply(str)
if (r1 != None) {
  val group = r1.get
  println(group)
} else {
  println("no")
}

如果 NumberMatcher 返回 Boolean,则你可以让它收到一些东西。在这种情况下,会发生以下情况:

str match { 
  case NumberMatcher() => println("yes")
  case _ => println("no")
}

变为

val r1 = NumberMatcher.unapply(str)
if (r1) {
  println("yes")
} else {
  println("no")
}

请注意,这是一个非常肤浅的解释。大小写匹配可以测试常量、有额外的保护条件、替代方案、递归使用 unapply、使用 unapplySeq 等。这里我只展示非常基本的用法来解决特定的问题问题。我强烈建议寻找有关模式匹配的更完整的解释。

If you want to return something with unapply, return it inside Some. Returning Boolean just tests if the match can be made or not.

Here is how a pattern matching is translated:

str match { 
  case NumberMatcher(group) => println(group)
  case _ => println("no")
}

Assuming NumberMatcher returns Option[...], it will do:

val r1 = NumberMatcher.unapply(str)
if (r1 != None) {
  val group = r1.get
  println(group)
} else {
  println("no")
}

If NumberMatcher returns Boolean, then you can have it receive something. In that case, this is what happens:

str match { 
  case NumberMatcher() => println("yes")
  case _ => println("no")
}

becomes

val r1 = NumberMatcher.unapply(str)
if (r1) {
  println("yes")
} else {
  println("no")
}

Note that this is a very superficial explanation. Case matches can test for constants, have additional guard conditions, alternatives, use unapply recursively, use unapplySeq, 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.

喜你已久 2024-08-21 11:40:22

再次查看这个示例
我引用

unapply 的返回类型应选择如下:
* 如果只是测试,则返回布尔值。例如,even()
* 如果返回单个T类型的子值,则返回一个Option[T]
* 如果要返回多个子值 T1,...,Tn,请将它们分组在可选元组 Option[(T1,...,Tn)] 中。

Take a look at this example again.
I quote

The return type of an unapply should be chosen as follows:
* If it is just a test, return a Boolean. For instance case even()
* If it returns a single sub-value of type T, return a Option[T]
* If you want to return several sub-values T1,...,Tn, group them in an optional tuple Option[(T1,...,Tn)].

旧时光的容颜 2024-08-21 11:40:22

当您定义 unapply 以返回 Boolean 时,您表明该模式没有任何要匹配(或绑定)的通配符。所以这个解提取器的 case 语句应该是 case NumberMatcher =>; println(str),并给它一个变量来填充是错误的。

或者,使 case NumberMatcher(group) => println(group) 您需要定义 unapply() 以返回 Option[String]

When you defined unapply to return a Boolean, you were indicating that the pattern doesn't have any wildcards to match (or bind). So the case statement for this unextractor should be case NumberMatcher => println(str), and giving it a variable to fill is wrong.

Alternatively, to make case NumberMatcher(group) => println(group) you need to define unapply() to return Option[String]

流绪微梦 2024-08-21 11:40:22
package com.tutorial.extracters
object ExtracterwithBooleanReturnType extends App {
import java.util.regex._
object NumberMatcher {
  def apply(x: String*) = {
    x
  }
  def unapply(x: String): Option[Boolean] = {
    val pat = Pattern.compile("\\d+")
    val matcher = pat.matcher(x)
    return Some(matcher.find)
 }
}

val strings = NumberMatcher("geo12", "neo493", "leo")
for (str <- strings) {
  str match {
  case NumberMatcher(group) => println(group)
  case _ => println("no")
}
}
}

我们也可以用上面的代码来实现

package com.tutorial.extracters
object ExtracterwithBooleanReturnType extends App {
import java.util.regex._
object NumberMatcher {
  def apply(x: String*) = {
    x
  }
  def unapply(x: String): Option[Boolean] = {
    val pat = Pattern.compile("\\d+")
    val matcher = pat.matcher(x)
    return Some(matcher.find)
 }
}

val strings = NumberMatcher("geo12", "neo493", "leo")
for (str <- strings) {
  str match {
  case NumberMatcher(group) => println(group)
  case _ => println("no")
}
}
}

we can achieve this with above code as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文