Skip to content

Commit b03a742

Browse files
committed
Another test
1 parent 96e50d0 commit b03a742

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

tests/run/errorhandling.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
breakTest
2+
optTest
3+
resultTest
4+
Person(Kostas,5)

tests/run/errorhandling/Test.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ def resultTest() =
6666
@main def Test =
6767
breakTest()
6868
optTest()
69-
resultTest()
69+
resultTest()
70+
parseCsvIgnoreErrors()

tests/run/errorhandling/break.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ object boundary:
1111
transparent inline def apply[T <: R, R](inline body: Label[T] ?=> R): R =
1212
val local = Label[T]()
1313
try body(using local)
14-
catch case ex: Break[T] @unchecked if ex.label eq local =>
15-
ex.value
14+
catch case ex: Break[T] @unchecked =>
15+
if ex.label eq local then ex.value
16+
else throw ex
1617

1718
end boundary
1819

tests/run/errorhandling/kostas.scala

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package optionMockup:
2+
import dotty.util.boundary
3+
object optional:
4+
transparent inline def apply[T](inline body: boundary.Label[None.type] ?=> T): Option[T] =
5+
boundary(Some(body))
6+
7+
extension [T](r: Option[T])
8+
transparent inline def ? (using label: boundary.Label[None.type]): T = r match
9+
case Some(x) => x
10+
case None => label.break(None)
11+
12+
import optionMockup.*
13+
14+
case class Person(name: String, age: Int)
15+
16+
object PersonCsvParserIgnoreErrors:
17+
def parse(csv: Seq[String]): Seq[Person] =
18+
for
19+
line <- csv
20+
columns = line.split(",")x
21+
parsed <- parseColumns(columns)
22+
yield
23+
parsed
24+
25+
private def parseColumns(columns: Seq[String]): Option[Person] =
26+
columns match
27+
case Seq(name, age) => parsePerson(name, age)
28+
case _ => None
29+
30+
private def parsePerson(name: String, age: String): Option[Person] =
31+
optional:
32+
Person(name, age.toIntOption.?)
33+
34+
def parseCsvIgnoreErrors() =
35+
println(PersonCsvParserIgnoreErrors.parse(Seq("Kostas,5", "George,invalid", "too,many,columns")).mkString("\n"))

0 commit comments

Comments
 (0)