Created
March 27, 2020 16:58
-
-
Save Blaisorblade/1835dbfa8b2d015a810f3d0cf24412b8 to your computer and use it in GitHub Desktop.
Try making `Elem` covariant in `N` — but this fails :-|
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._ | |
final case class Col[Key, Value](data: Array[Value]) | |
object Frame { | |
// Covariant version of Tuple.Elem | |
/** Type of the element a position N in the tuple X */ | |
type Elem[+X <: Tuple, +N <: Int] = X match { | |
case x *: xs => | |
N match { | |
case 0 => x | |
case S[n1] => Elem[xs, n1] | |
} | |
} | |
// | |
extension { | |
inline def [This <: NonEmptyTuple](t: This).mapply(n: Int): Elem[This, n.type] = { | |
import scala.runtime.DynamicTuple | |
DynamicTuple.dynamicApply[This, n.type](t, n).asInstanceOf[Elem[This, n.type]] | |
} | |
} | |
object Types { | |
type IndexOf[F <: Tuple, Key] <: Int = | |
F match { | |
case Col[Key, v] *: cols => 0 | |
case Col[k, v] *: cols => S[IndexOf[cols, Key]] | |
} | |
type Get[F <: Tuple, Key] = Elem[F, IndexOf[F, Key]] | |
} | |
inline def get[Key, F <: NonEmptyTuple](frame: F) : Types.Get[F, Key] = | |
frame.mapply(constValue[Types.IndexOf[F, Key]]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works in the presentation compiler, but fails in later phases when checking variance: