Last active
August 29, 2015 14:27
-
-
Save ahoy-jon/4214ecfc6cb0ab2f0fad to your computer and use it in GitHub Desktop.
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
package testGeneric | |
import scala.language.higherKinds | |
import scalaz.Functor | |
object TestLabelledGeneric { | |
case class Ahoy(name:String, y:Int, l:Int) | |
//val name:String = "hello" | |
val oName:Option[String]= Option("hello") // None | |
val oY:Option[String] = None | |
import shapeless._ | |
import syntax.singleton | |
import singleton._ | |
val a = 'name ->> Option("hello") :: 'y ->> Option(1) :: HNil | |
val b = Option('name ->> "hello") :: Option('y ->> 1) :: HNil | |
import labelled.{ FieldType, field } | |
import scalaz.Functor | |
object f extends Poly1 { | |
implicit def kv[K, V,A[_]](implicit app:Functor[A]): Case.Aux[ | |
FieldType[K, A[V]], | |
A[FieldType[K, V]] | |
] = | |
at(a => app(a)(field[K](_))) | |
} | |
// If I try to use Witness.`"name"`.T directly in Res, I have a "not accessible type" error | |
val name = Witness.`'name` | |
val y = Witness.`'y` | |
type Res = Option[FieldType[name.T, String]] :: Option[FieldType[y.T ,Int]] :: HNil | |
def assertTypedEquals[A](expected: A, actual: A): Unit = assert(expected == actual) | |
import scalaz.std.option._ | |
implicitly[Functor[Option]] | |
assertTypedEquals[Res](b, a.map(f)) | |
val generic = the[LabelledGeneric[Ahoy]] | |
import shapeless.contrib.scalaz._ | |
import scalaz.Applicative | |
import shapeless.test.typed | |
type Result = FieldType[name.T,String] :: FieldType[y.T,Int] :: HNil | |
typed[Option[Result]](sequence(a.map(f))) | |
//typed[Result](generic.to(Ahoy("hello",1))) | |
//sequence(a.map(f)).map(generic.from) | |
def build[A[_]](name:A[String],y:A[Int], l:A[Int])(implicit app:Applicative[A]):A[Ahoy] = { | |
app.map(sequence(('name ->> name :: 'y ->> y :: 'l ->> l :: HNil).map(f)))(generic.from) | |
} | |
//@caseClassBuild def build[A[_]](name:A[String],y:A[Int])(implicit app:Applicative[A]):A[Ahoy] = ??? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment