Skip to content

Conversation

@PapyChacal
Copy link

@PapyChacal PapyChacal commented Dec 4, 2025

Hello, just a tiny signature-change that would improve our quality of life using fastparse in Scala3 🙂

Scala3 now has union types, which are perfect to express the type of the either-or parser combinator | but is unused in fastparse.

In our code, this leads to cases like:

class A <: Base
class B <: Base

def f(x: A | B): Something

def parseA: P[A]
def parseB: P[B]

def fancyParse: P[Something] = (parseA | parseB).map(
(x: Base) => f(x.asInstanceOf[A|B])
)

The asInstanceOf is required to bridge (parseA | parseB), currently typed to some common supertype, here P[Base], where it could now simply be typed P[A|B], which this PR offers 🙂 I just wanted to clarify the motivation, in case I missed an existing alternative or what not!

It sounds as non-intrusive as can be to me, not sure if I'm missing something though; similarly, I saw type tests specifically for implicit sequencers and everything, but none for the type of combined parsers. Tell me if you feel like this requires specific tests here!

@PapyChacal PapyChacal changed the title Use Scala3's union type for stronger typing on |. Use Scala3's union type for stronger typing on | Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant