Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ lazy val root = project
description := "Example sbt project that compiles using Scala 3",
version := "0.1.0",
scalaVersion := "3.1.3",
scalacOptions ++= Seq("-deprecation"),
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test
)
8 changes: 4 additions & 4 deletions src/main/scala/GivenInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ object GivenInstances:

def apply[A](using parser: StringParser[A]): StringParser[A] = parser

private def baseParser[A](f: String Try[A]): StringParser[A] = new StringParser[A] {
private def baseParser[A](f: String => Try[A]): StringParser[A] = new StringParser[A] {
override def parse(s: String): Try[A] = f(s)
}

given stringParser: StringParser[String] = baseParser(Success(_))
given intParser: StringParser[Int] = baseParser(s Try(s.toInt))
given intParser: StringParser[Int] = baseParser(s => Try(s.toInt))

given optionParser[A](using parser: => StringParser[A]): StringParser[Option[A]] = new StringParser[Option[A]] {
override def parse(s: String): Try[Option[A]] = s match
case "" Success(None) // implicit parser not used.
case str parser.parse(str).map(x Some(x)) // implicit parser is evaluated at here
case "" => Success(None) // implicit parser not used.
case str => parser.parse(str).map(x => Some(x)) // implicit parser is evaluated at here
}

def test(): Unit =
Expand Down