#scala cats-mtl submarine error handling .. inspired by
Thanh Le (lenguyenthanh)'s talk at #scaladays .. here is a working #scastie

https://scastie.scala-lang.org/KcQYZKLgRM6NIwAtdPiYpg

Uses scalacli features and a 3.8.nightly (also tested with 3.7.4)

references:
1. https://github.com/lenguyenthanh/talks/blob/main/submarine/code/example5.scala
2. https://www.youtube.com/watch?v=nNqx2HiL7cc

Scastie - An interactive playground for Scala.

//> using scala 3.8.1 //> using option -language:experimental.saferExceptions //> using option -language:experimental.captureChecking // Describes computations that can fail with error `E` final class CanThrow[-E] // Internal exception used to jump to the catch block private final case class Break[E](value: E, label: CanThrow[E]) extends RuntimeException(null, null, false, false) // Library version of `throw` def failWith[A](a: A)(using l: CanThrow[A]): Nothing = throw Break(a, l) // Provides a way to catch errors. object Catch: def apply[A, E](op: CanThrow[E] ?=> A)(default: E => A): A = given label: CanThrow[E] = new CanThrow() try op catch case Break(value, `label`) => default(value) // Error types sealed trait Error extends Exception case class NegativeNumber(i: Int) extends Error case class InvalidInput(s: String) extends Error // sqrt can fail if its argument is negative. def sqrt(i: Int)(using CanThrow[NegativeNumber]): Double = if i >= 0 then math.sqrt(i) else failWith(NegativeNumber(i)) // parse can fail if its argument is not a valid int. def parse(s: String)(using CanThrow[InvalidInput]): Int = scala.util.Try(s.toInt).getOrElse(failWith(InvalidInput(s))) // Composing both widens the type of the error to the parent of `NegativeNumber` and `InvalidInput`. def composed(s: String)(using CanThrow[Error]): Double = sqrt(parse(s)) // I like this better - auto-widening is nice, but note how `Error` is not sealed: `composedSubtype` // forces me to deal with error cases that don't exist. def composedBetter(s: String)(using CanThrow[NegativeNumber | InvalidInput] ): Double = sqrt(parse(s)) def printSqrt(s: String): Unit = Catch(println(composedBetter(s))): case NegativeNumber(i) => println(s"Negative number: $i") case InvalidInput(s) => println(s"Invalid input: '$s'") @main def run = printSqrt("abc") printSqrt("-4") printSqrt("4")

Just watched Martin Odersky keynote at ScalaDays 2025, about Scala future Capabilities, and he's pretty convincing.

It shows what could be the shape of Scala programming in the coming years, and what Scala 3 was aiming at.

(he was less convincing when talking about project migration from effect systems, because it's another way of functional programming)

#Scala #ScalaDays #ScalaDays2025

https://www.youtube.com/watch?v=p-iWql7fVRg

Martin Odersky: Where Are We With Scala's Capabilities? [Scala Days 2025 Keynote]

YouTube

Scala Days 2025 videos are out:
https://www.youtube.com/@ScalaDaysConferences/videos

If you don't know what to watch, check out talks on Scala Capabilities, an upcoming language feature. That's big.

#Scala #ScalaDays

Scala Days Conferences

Videos recorded at Scala Days Conferences Follow us on Twitter @ScalaDays or visit our website for more information http://scaladays.org

YouTube

There are tons of amazing events waiting for you in the Scala community!

Just 2 weeks ago, our experts Magda Stożek & Adam Warski performed on the Scala Days stage, and in March we will meet again at the Scalar Conference! 🐠

📅 Save the date: March 26-27, 2026

🎟️ Get your Super Early Bird ticket - available only until September 7th: https://www.scalar-conf.com/tickets

#scalarconference #scala #functionalprogramming #scaladays

Post-#ScalaDays hike! Just an hour away from Lausanne :)

Speaking of Scala conferences… if anything inspired you at ScalaDays, maybe it would make a good talk for the Scalar Conference? The c4p is closer than you think ;)

heading back home today. thanks Scala Center, EPFL folks, speakers, volunteers, participants, and all the sponsors for an amazing week of #ScalaDays and hallway conversations
https://fosstodon.org/@scala_lang/115050591372195599
Scala (@[email protected])

Attached: 2 images ✨ Such a smooth and productive start to the amazing conference would not be possible without our nice, motivated and skilled volunteers who were working tirelessly today to ensure the world-class experience for everyone! Thank you so much for your hard work 🙏

Fosstodon
Heading home from #Scaladays. Thanks for a good event, nice talks and good conversations.

here's the slide deck for 'sbt 2.0: go big' talk I gave at #ScalaDays today

many thanks everyone who showed up to the talk or had conversation with me at the conference
https://www.slideshare.net/slideshow/sbt-2-0-go-big-scala-days-2025-edition/282592302 #Scala

Hello world!
#scala #scaladays

Next up, a talk by Anna Herlihy of LAMP and DIAS about macro-free type-safe database queries.

Having refactored my fair share of bindings to NoSQL DBs, I am definitely looking forwards to understanding better how to create universal interfaces that cover both NoSQL query languages and SQL!

#ScalaDays #ScalaDays2025 #Scala

11/🧵