samedi 18 mars 2023

Type negation in Scala 3

Is it possible to implement fail proof Not[A] type in Scala 3? Here is a question asked some years ago: how-can-i-have-a-negation-type-in-scala. Unfortunately, a solution based on 'not given A <:< B' fails if either is an abstract type: absence of evidence is not an evidence of absence fallacy. Technically, with macros, it should be possible to verify the type of the expression and produce an error if full type information is not known. However, I'd like it also to work not just when 'not A <:< B', but only if A with B is effectively Nothing: either type is sealed/final and not possible to mix into the other. On the other hand, in that scenario, full information about both types shouldn't be required: if A is final, any upper bound on B which is neither a type variable nor A should work.

Motivation:

sealed trait Marker
sealed trait A[X]
final class Special[X <: Marker] extends A[X]
final class Generic[X :Not[Marker]#Proof] extends A[X]

In the example above, If I have an a :A[Marker], then I know for sure that a.asInstanceOf[Special[Marker]] is sound.





Aucun commentaire:

Enregistrer un commentaire