Created
April 1, 2024 06:59
-
-
Save chuwy/1f2cf3c4f3d9d1ab07fe480744d1ae22 to your computer and use it in GitHub Desktop.
A Scala 3 match type that checks that all elements of a tuple match a certain (sub)type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.compiletime.ops.boolean.&& | |
type AllSubtypesOfMatch[T <: Tuple, Super] <: Boolean = T match | |
case EmptyTuple => | |
true | |
case h *: t => | |
h match | |
case Super => AllSubtypesOfMatch[t, Super] | |
type AllSubtypesOf[T <: Tuple, Super] = AllSubtypesOfMatch[T, Super] =:= true | |
def processErrors[T <: Tuple](errors: T)(using AllSubtypesOf[T, Throwable]) = () | |
processErrors((new RuntimeException("foo"), new IllegalAccessError("boom"))) // ok | |
// processErrors((new RuntimeException("foo"), new IllegalAccessError("boom"), "foo")) // not ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another way to ensure consistency of the tuple elements:
Foo[_]
def ofTuple[T <: Tuple: Tuple.IsMappedBy[Foo]](tup: T)