0

I am a Scala noob reading through a parsing library, and have reached some syntax I do not understand:

def parseA[_: P] = P("a")

val Parsed.Success(value, successIndex) = parse("a", parseA(_))

I want to be able to combine these lines into one, ie

val Parsed.Success(value, successIndex) = parse("a", P("a"))

but this gives a compile error:

Error:(8, 61) overloaded method value P with alternatives:
  [T](t: fastparse.P[T])(implicit name: sourcecode.Name, implicit ctx: fastparse.P[_])fastparse.P[T] <and>
  => fastparse.ParsingRun.type
 cannot be applied to (String)
Error occurred in an application involving default arguments.
    val Parsed.Success(value, successIndex) = parse(source, P("a"))

How should this line be written? And can you name the syntax concepts involved to maximise my learning?

Mazerunner72
  • 613
  • 2
  • 6
  • 16

1 Answers1

1

_: P is the same as (implicit ctx: P[_]), that means that method is asking for an implicit parameter of type P[_] (the underscore means that it does not care for the inner type. See What are all the uses of an underscore in Scala?).
P("a") is calling this method, which requires such implicit in scope, and that is why in your second example it fails to compile, because it did not find the implicit parameter.
The features sued here are implicits, existential types & macros...

All of them are very advanced techniques. If you are just starting, I would suggest to leave them for latter.
Implicits are very important and useful, I would start from there, but first make sure you feel comfortable with "normal" Scala (whatever that means).


For the second question, I think this should work.

def program[_: P] = parse("a", P("a"))
val Parsed.Success(value, successIndex) = program
  • This generated some compile errors: first line: `Error:(27, 37) type mismatch; found : fastparse.package.P[Unit] (which expands to) fastparse.ParsingRun[Unit] required: fastparse.P[_] => fastparse.P[?] (which expands to) fastparse.ParsingRun[_] => fastparse.ParsingRun[?] Error occurred in an application involving default arguments. def program[_: P] = parse("a", P("a"))` also second line said `could not find implicit value` – Mazerunner72 May 22 '19 at 17:15
  • @Mazerunner72 uhm... many things happening here, I really would not bother on _"refactoring"_ to a single line, specially if it worked before. Also, `fastparse` introduces its own **DSL**, I have not used the library before, and it seems I would need to read quite a bit to understand what is happening, so if you are having troubles with the library you could opening a new question explicitly mentioning in the title you are using that library, so people who knows it can see it and hopefully answer it. Also, you may try asking tn **gitter** instead there is even a channel just for `fastparse`. – Luis Miguel Mejía Suárez May 22 '19 at 18:00