“scala 不是封闭类”

发布于 2024-10-19 08:34:56 字数 2528 浏览 2 评论 0原文

编译此规范时:

import org.specs.Specification
import org.specs.matcher.extension.ParserMatchers

class ParserSpec extends Specification with ParserMatchers {
  type Elem = Char

  "Vaadin DSL parser" should {
    "parse attributes in parentheses" in {
      DslParser.attributes must(
        succeedOn(stringReader("""(attr1="val1")""")).
          withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String"))))
    }
  }
}

我收到以下错误:

ParserSpec.scala:21
error: scala is not an enclosing class
withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String"))))
           ^

我根本不理解这里的错误消息。为什么会出现呢?

Scala 版本是 2.8.1,specs 版本是 1.6.7.2。

DslParser.attributes 具有类型 Parser[Map[String, AttrVal]] 并定义了组合器 succeedOnwithResult如下:

trait ParserMatchers extends Parsers with Matchers {
  case class SucceedOn[T](str: Input,
                          resultMatcherOpt: Option[Matcher[T]]) extends Matcher[Parser[T]] {
    def apply(parserBN: => Parser[T]) = {
      val parser = parserBN
      val parseResult = parser(str)
      parseResult match {
        case Success(result, remainingInput) =>
          val succParseMsg = "Parser "+parser+" succeeded on input "+str+" with result "+result
          val okMsgBuffer = new StringBuilder(succParseMsg)
          val koMsgBuffer = new StringBuilder(succParseMsg)
          val cond = resultMatcherOpt match {
            case None =>
              true
            case Some(resultMatcher) =>
              resultMatcher(result) match {
                case (success, okMessage, koMessage) =>
                  okMsgBuffer.append(" and ").append(okMessage)
                  koMsgBuffer.append(" but ").append(koMessage)
                  success
              }
          }
          (cond, okMsgBuffer.toString, koMsgBuffer.toString)
        case _ =>
          (false, "Parser succeeded", "Parser "+parser+": "+parseResult)
      }
    }

    def resultMust(resultMatcher: Matcher[T]) = this.copy(resultMatcherOpt = Some(resultMatcher))

    def withResult(expectedResult: T) = resultMust(beEqualTo(expectedResult))

    def ignoringResult = this.copy(resultMatcherOpt = None)
  }

  def succeedOn[T](str: Input, expectedResultOpt: Option[Matcher[T]] = None) =
    SucceedOn(str, expectedResultOpt)

  implicit def stringReader(str: String): Reader[Char] = new CharSequenceReader(str)
}

When compiling this specification:

import org.specs.Specification
import org.specs.matcher.extension.ParserMatchers

class ParserSpec extends Specification with ParserMatchers {
  type Elem = Char

  "Vaadin DSL parser" should {
    "parse attributes in parentheses" in {
      DslParser.attributes must(
        succeedOn(stringReader("""(attr1="val1")""")).
          withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String"))))
    }
  }
}

I get the following error:

ParserSpec.scala:21
error: scala is not an enclosing class
withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String"))))
           ^

I don't understand the error message here at all. Why could it appear?

Scala version is 2.8.1, specs version is 1.6.7.2.

DslParser.attributes has type Parser[Map[String, AttrVal]] and the combinators succeedOn and withResult are defined as follows:

trait ParserMatchers extends Parsers with Matchers {
  case class SucceedOn[T](str: Input,
                          resultMatcherOpt: Option[Matcher[T]]) extends Matcher[Parser[T]] {
    def apply(parserBN: => Parser[T]) = {
      val parser = parserBN
      val parseResult = parser(str)
      parseResult match {
        case Success(result, remainingInput) =>
          val succParseMsg = "Parser "+parser+" succeeded on input "+str+" with result "+result
          val okMsgBuffer = new StringBuilder(succParseMsg)
          val koMsgBuffer = new StringBuilder(succParseMsg)
          val cond = resultMatcherOpt match {
            case None =>
              true
            case Some(resultMatcher) =>
              resultMatcher(result) match {
                case (success, okMessage, koMessage) =>
                  okMsgBuffer.append(" and ").append(okMessage)
                  koMsgBuffer.append(" but ").append(koMessage)
                  success
              }
          }
          (cond, okMsgBuffer.toString, koMsgBuffer.toString)
        case _ =>
          (false, "Parser succeeded", "Parser "+parser+": "+parseResult)
      }
    }

    def resultMust(resultMatcher: Matcher[T]) = this.copy(resultMatcherOpt = Some(resultMatcher))

    def withResult(expectedResult: T) = resultMust(beEqualTo(expectedResult))

    def ignoringResult = this.copy(resultMatcherOpt = None)
  }

  def succeedOn[T](str: Input, expectedResultOpt: Option[Matcher[T]] = None) =
    SucceedOn(str, expectedResultOpt)

  implicit def stringReader(str: String): Reader[Char] = new CharSequenceReader(str)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

听,心雨的声音 2024-10-26 08:34:56

当编译器真正尝试发出类型错误或类型推断失败信号时,可能会出现此消息。这是 scalac 中的一个错误(或一系列错误)。

定位问题,逐步添加显式类型和类型参数;将复杂的表达式分解为更小的子表达式。

为了获得奖励积分,请制作一个独立的示例并提交错误。

This message can occur while the compiler is really trying to signal a type error or a type inference failure. It's a bug (or family of bugs) in scalac.

To locate the problem, progressively add in explicit types and type arguments; break complex expressions into smaller subexpressions.

For bonus points, produce a standalone example and file a bug.

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