Created
April 14, 2020 13:47
-
-
Save noelwelsh/e3ccdb6711130aee0caae215a6a37825 to your computer and use it in GitHub Desktop.
An HList in Dotty (which doesn't compile)
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._ | |
import scala.compiletime.ops.any | |
// This is a standard HList plus data is keyed by a type K | |
sealed trait HList { | |
def :::[K <: AnyVal, V](data: V): HCons[K, V, this.type] = HCons(data, this) | |
inline def contains[K <: AnyVal]: Boolean = | |
inline this match { | |
case HNil => false | |
case h: HCons[k, v, t] => | |
inline if(constValue[any.==[K, k]]) true | |
else h.tail.contains[K] | |
} | |
} | |
final case class HCons[K <: AnyVal, V, T <: HList](data: V, tail: T) extends HList | |
case object HNil extends HList | |
object HListExamples extends App { | |
val list = HNil.:::["name", String]("Dotty") | |
// Compilation fails with | |
// | |
// [error] 21 | assert(list.contains["name"] == true) | |
// [error] | ^^^^^^^^^^^^^^^^^^^^^ | |
// [error] |cannot reduce inline match with | |
// [error] | scrutinee: { | |
// [error] | HList_this | |
// [error] |} : (HList_this : (HListExamples.list : HCons[("name" : String), String, HNil.type]) | |
// [error] | ) | |
// [error] | patterns : case HNil | |
// [error] | case h @ _:HCons[k @ _, v @ _, t @ _] | |
// [error] | This location contains code that was inlined from HList.scala:8 | |
assert(list.contains["name"] == true) | |
assert(list.contains["segfaults"] == false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment