… } 动态提取器? 这很不-6ren">
gpt4 book ai didi

scala - Scala 中的动态提取器

转载 作者:行者123 更新时间:2023-12-02 00:00:05 29 4
gpt4 key购买 nike

我不喜欢提取器的一件事是它们不能有参数。所以我不能在以下位置使用像 Param 这样的提取器:

req match { case Param("foo")(foo) => … }

动态提取器?

这很不幸,我希望有一天它会改变,但今天早上我想我可以通过使用动态特征来修复它。

object Params extends Dynamic {
def selectDynamic(name: String) = new {
def unapply(params: Map[String, String]): Option[String] = params.get(name)
}
}

…希望这能让我在模式匹配语句中使用参数,如下所示:

req match { case Params.Foo(value) => 
// matching Map("Foo" -> "Bar"), extracting "Bar" in value

不起作用

...但它不起作用。编译器似乎仍然感到困惑。

scala> Map("Foo" -> "bar") match { case Params.Foo(value) => value }
<console>:10: error: value applyDynamic is not a member of object Params
error after rewriting to Params.<applyDynamic: error>("Foo")
possible cause: maybe a wrong Dynamic method signature?
Map("Foo" -> "bar") match { case Params.Foo(value) => value }
^
<console>:10: error: not found: value value
Map("Foo" -> "bar") match { case Params.Foo(value) => value }
^

这让我感到惊讶,因为

object Params {
object Foo {
def unapply{params: Map[String, String]): Option[String] = …
}
}

效果很好。另外,如果我首先将 Params.Foo 分配给一个变量,一切都会好的:

scala> val Foo = Params.Foo
Foo: AnyRef{def unapply(params: Map[String,String]): Option[String]} = Params$$anon$1@f2106d8

scala> Map("Foo" -> "bar") match { case Foo(value) => value }
warning: there were 1 feature warning(s); re-run with -feature for details
res2: String = bar

这应该被视为错误吗?

最佳答案

规范的答案是 Can extractors be customized with parameters in the body of a case statement (or anywhere else that an extractor would be used)?

但是the hacking blog建议将参数作为任意名称传递给动态选择,如问题中所尝试的那样。

$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> class X(pattern: String) { val RegExp = new { def unapplySeq(s: String) = pattern.r.unapplySeq(s) } }
defined class X

scala> import language._
import language._

scala> case object p extends Dynamic { def selectDynamic(pattern: String) = new X(pattern) }
defined object p

scala> "abcdef" match { case p.`.*(b.*d).*`.RegExp(s) => s }
res0: String = bcd

需要额外的选择,因为 a crashing bug 2.11 中的错误与问题中显示的 2.10 错误不同:

scala> class RegExp(pattern: String) { def unapplySeq(s: String) = pattern.r.unapplySeq(s) }
defined class RegExp

scala> case object p extends Dynamic { def selectDynamic(pattern: String) = new RegExp(pattern) }
defined object p

scala> "abcdef" match { case p.`.*(b.*d).*`(s) => s }
java.lang.NullPointerException
at scala.tools.nsc.typechecker.PatternTypers$PatternTyper$class.inPlaceAdHocOverloadingResolution(PatternTypers.scala:68)

2.10 中的工作示例:

$ scala210 -language:_
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_95).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :pa
// Entering paste mode (ctrl-D to finish)

class X(key: String) { val get = new { def unapply(params: Map[String, String]): Option[String] = params.get(key) }}
object Params extends Dynamic {
def selectDynamic(name: String) = new X(name)
}

// Exiting paste mode, now interpreting.

defined class X
defined module Params

scala> Map("Foo" -> "bar") match { case Params.Foo.get(value) => value }
res0: String = bar

这与问题末尾显示的内容类似,但很明显可以通过这种方式使用动态选择。

关于scala - Scala 中的动态提取器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24451337/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com