Skip to content

Instantly share code, notes, and snippets.

@larsrh
Created April 12, 2012 14:48
Show Gist options
  • Save larsrh/2367848 to your computer and use it in GitHub Desktop.
Save larsrh/2367848 to your computer and use it in GitHub Desktop.
An alternative approach to index-access in HLists
// Context <https://gist.github.com/2346456>
val hlist = 1 :: "foo" :: 3.0f :: HNil
val nones = HStream.const(None)
val wrapSome = new (Id ~> Some) { def apply[T](t: T) = Some(t) }
val somelist = hlist.transform(wrapSome)
val stream = somelist +: nones
// `x` is the name of the value in `Some`
scala> stream(_0).x
res2: Int = 1
scala> stream(_1).x
res3: java.lang.String = foo
// this doesn't work as expected, ...
scala> stream(_2)
<console>:21: error: cyclic aliasing or subtyping involving type Unapplied
// ... but this does
scala> stream(_2).x
res4: Float = 3.0
scala> stream(_3).x
<console>:22: error: type mismatch;
found : scalaz.typelevel.Succ[scalaz.typelevel.Succ[scalaz.typelevel.Succ[scalaz.typelevel.Zero.type]]]#scala.Unapplied[Some[Int],[N(in type λ) <: scalaz.typelevel.Nat]N(in type λ)#Unapplied[Some[java.lang.String],[(some other)N(in type λ) <: scalaz.typelevel.Nat](some other)N(in type λ)#Unapplied[Some[Float],[B]None.type]]]
required: ?{val x: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
are possible conversion functions from scalaz.typelevel.Succ[scalaz.typelevel.Succ[scalaz.typelevel.Succ[scalaz.typelevel.Zero.type]]]#Unapplied[Some[Int],[N <: scalaz.typelevel.Nat]... res0(_3).x
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment