Created
December 1, 2016 15:14
-
-
Save joroKr21/ab32092ab1e3bc8055df13daba77204b to your computer and use it in GitHub Desktop.
Materialize lenses for all keys in a record.
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 shapeless._ | |
import shapeless.labelled._ | |
import shapeless.ops.hlist._ | |
import shapeless.ops.record._ | |
import shapeless.syntax.singleton._ | |
object MkLenses { | |
object mkLenses extends Poly1 { | |
implicit def mkLens[R <: HList, K](implicit mk: MkRecordSelectLens[R, K]) = | |
at[K](_ => mk()) | |
} | |
trait Dummy[-A] | |
object Dummy { | |
private val instance = new Dummy[Any] { } | |
def apply[A]: Dummy[A] = instance | |
} | |
def lenses[R <: HList, K <: HList]( | |
dummy: Dummy[R] | |
)(implicit | |
keys: Keys.Aux[R, K], | |
lensesFor: Mapper[mkLenses.type, K] | |
) = lensesFor(keys()) | |
val key = 'key.witness | |
type Rec = FieldType[key.T, Int] :: HNil | |
implicitly[MkRecordSelectLens[Rec, key.T]] | |
lenses(Dummy[Rec]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment