I made the following example of an ad-hoc record built on match types + opaque type over
Map[String, Any] in scala 3:
One thing I don’t like about the example is that if you try to get from an empty record, you get a bad error message:
[error] |Note: a match type could not be fully reduced: [error] | [error] | trying to reduce example.RecMap.Record.Rec.HasKey[EmptyTuple, Int] [error] | failed since selector EmptyTuple [error] | matches none of the cases [error] | [error] | case (Int, t) *: _ => t [error] | case _ *: t => example.RecMap.Record.Rec.HasKey[t, Int]
What I would like to be able to write is something like:
type HasKey[A <: Tuple, K] = A match case (K, t) *: _ => t case _ *: t => HasKey[t, K] case EmptyTuple => error(s"Record does not have a key of type $K")
There are some issues here: the normal error function returns a value, not a type, so that has some issues, but maybe we could have a built in match type like
ErrorType[M <: String & Singleton] or something which during reducing would report an error message giveng by the singleton type?
Maybe there is another way to solve this, but the goal is custom error messages in match type reduction failures.