Learning Scala 3.
how can i make this code type-match and compile?
trait Key {
type Value
}
object Name extends Key {
type Value = String
}
object Age extends Key {
type Value = Int
}
type DB = (k: Key) => Option[k.Value]
val dbImpl: DB = (k: Key) => {
k match {
case Name => Some("abc") // this does not compile, how can i make it sniff Value type is String automatically?
case Age => None
}
}
thanks
As a workaround you can try match types
or
I'm reminding that
or
or
will not work.
scala 3 map tuple to futures of tuple types and back
Scala 3: typed tuple zipping
Express function of arbitrary arity in vanilla Scala 3
Shapeless3 and annotations
How to get match type to work correctly in Scala 3
Another option is type classes
One more option is inlining and using
scala.compiletime.summonFromThe easiest is to make
Valuea type parameter rather than type memberThis implementation compiles while
or
or
doesn't. (Scala 3.2.2)
Aleksander Boruch-Gruszecki. GADTs in Dotty https://www.youtube.com/watch?v=VV9lPg3fNl8
Dale Wijnand. Space Engine for Pattern Matching https://www.youtube.com/watch?v=yaxJPIsy4Js