Def parser[_: P] = P( "hello" )

Hi there,

Sorry that I even do not know how to title the question so I put the puzzle itself to the topic title.

I am picking up fastparse:

Easy Parsing with Parser Combinators (lihaoyi.com)

But got puzzled with the 1st syntax :frowning:

def parser[_: P] = P( "hello" )

Why do we need the ‘_’ here ?
It seems like a type wildcard, but I don’t understand why we need it here.

Fastparse is fast but it has a bit of tricky api due to use of macros. Also it will likely need an api change to port to scala 3.

There is a new library:

Which is a bit slower than fastparse 2 but it don’t use macros but supports all the common parser combinators. It also already works in scala 2 and 3.

You might want to check it out.

The short answer is that it’s syntactic sugar for

def parser[T](implicit ev: P[Any]) = P( "hello" )

It’s actually a kind of irregularity that scala 2 allows a wildcard in that position, and fastparse exploits that to allow for a less bloated definition of its parser rules.

2 Likes