Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Scala 3 union types don't always play nicely with choice #455

Open
@FlorianCassayre

Description

@FlorianCassayre
  • Scala:3.X
  • scala-parser-combinators:2.1.1

Consider the following motivating example:

importscala.util.parsing.combinator.ParsersenumToken {caseA()caseB()caseC()}objectParserextendsParsers {overridetypeElem=TokendeftokenA:Parser[Token.A]=???deftokenB:Parser[Token.B]=???deftokenC:Parser[Token.C]=???deftokenABC:Parser[Token.A|Token.B|Token.C]=    tokenA| tokenB| tokenC// error}

Unfortunately the definitiontokenABC doesn't compile:

Found:    Parser[Token]Required: Parser[Token.A | Token.B | Token.C]    tokenA | tokenB | tokenC

A workaround is to ascribetokenA to the desired union type:

deftokenABC:Parser[Token.A|Token.B|Token.C]=  (tokenA:Parser[Token.A|Token.B|Token.C])| tokenB| tokenC

However this looks unnatural and arguably not very intuitive.

If we look at the source, this is how the choice operator is defined:

def| [U>:T](q:=>Parser[U]):Parser[U]

And it is now evident why the code in the above example fails to compile. The obvious change would be to update the signature to:

def| [U](q:=>Parser[U]):Parser[T|U]

Which would definitely break cross compatibility and therefore wouldn't be a viable solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp