how to use shapeless to detect field type annotation - scala

I am trying to gather the fields of a case class that have a particular annotations at compile time using shapeless. I tried to play around the following snippet, but it did not work as expected (output nothing instead of printing "i"). How can I make it work ?
import shapeless._
import shapeless.labelled._
final class searchable() extends scala.annotation.StaticAnnotation
final case class Foo(#searchable i: Int, s: String)
trait Boo[A] {
def print(a: A): Unit
}
sealed trait Boo0 {
implicit def hnil = new Boo[HNil] { def print(hnil: HNil): Unit = () }
implicit def hlist[K <: Symbol, V, RL <: HList](implicit b: Boo[RL]): Boo[FieldType[K, V] :: RL] =
new Boo[FieldType[K, V] :: RL] {
def print(a: FieldType[K, V] :: RL): Unit = {
b.print(a.tail)
}
}
}
sealed trait Boo1 extends Boo0 {
implicit def hlist1[K <: Symbol, V, RL <: HList](implicit annot: Annotation[searchable, K], witness: Witness.Aux[K], b: Boo[RL]): Boo[FieldType[K, V] :: RL] =
new Boo[FieldType[K, V] :: RL] {
def print(a: FieldType[K, V] :: RL): Unit = {
Console.println(witness.value.name)
b.print(a.tail)
}
}
}
object Boo extends Boo1 {
implicit def generics[A, HL <: HList](implicit iso: LabelledGeneric.Aux[A, HL], boo: Boo[HL]): Boo[A] =
new Boo[A] {
def print(a: A): Unit = {
boo.print(iso.to(a))
}
}
}
implicitly[Boo[Foo]].print(Foo(1, "2"))

Looking at the macro of Annotation, it rejects type that is not a product or coproduct straight up
val annTreeOpts =
if (isProduct(tpe)) { ... }
else if (isCoproduct(tpe)) { ... }
else abort(s"$tpe is not case class like or the root of a sealed family of types")
this is quite unfortunate, as collecting type annotations at per field symbol level could be quite useful sometimes.
There is another type class Annotations defined in the same file that can actually collect particular annotations on field into an HList. However problem is the field information is totally lost. There is a clumsy way to hack things together to serve my use case...
// A is our annotation
// B is our result type
// C is our case class with some fields annotated with A
def empty: B = ???
def concat(b1: B, b2: B): B = ???
def func(a: A, nm: String): B = ???
object Collector extends Poly2 {
implicit def some[K <: Symbol](implicit witness: Witness.Aux[K]) =
at[B, (K, Some[A])] { case (b, (_, a)) => concat(b, func(a.get, witness.value.name)) }
implicit def none[K <: Symbol] = at[B, (K, None.type)] { case (b, _) => b }
}
def collect[HL <: HList, RL <: HList, KL <: HList, ZL <: HList](implicit
iso: LabelledGeneric.Aux[C, HL]
, annot: Annotations.Aux[A, C, RL]
, keys: Keys.Aux[HL, KL]
, zip: Zip.Aux[KL :: RL :: HNil, ZL]
, leftFolder: LeftFolder.Aux[ZL, B, Collector.type, B]): B = {
zip(keys() :: annot() :: HNil).foldLeft(empty)(Collector)
}

Related

Scala case classes and recursive reflection

Given 2 Scala case classes
case class Bar(x: Int)
case class Foo(b: Bar, z: Double)
I have a piece of code that prints the types of Foo fields using reflection:
import scala.reflect.runtime.universe._
def f[T: TypeTag] = typeOf[T].members.filter(!_.isMethod)
and I call it like f[Foo] and f[Bar]. Calling the former returns a List[Type] as [Bar, Double].
How can I call f on the first element of the list? Equivalently, how can I print types recursively when Foo has a custom class Bar? Equivalently how can I get from Bar as Type a Bar.type?
Many thanks
You don't actually need the type variable T in f. You can define it like this (as Dima suggested in the comments):
def f(t: Type) =
t.members.filter(!_.isMethod).map(_.typeSignature)
To use this to recursively print a type:
def printTypesRecursive(t: Type, prefix: String = ""): Unit = {
println(prefix + t)
f(t).foreach(printTypesRecursive(_, prefix + " "))
}
printTypesRecursive(typeOf[Foo])
Output:
Foo
Double
Bar
Int
Equivalently how can I get from Bar as Type a Bar.type?
Bar.type is the type of companion object
Class companion object vs. case class itself
I need something like f[f[Foo].head]
I guess you have here some confusion between compile-time and runtime
Runtime vs. Compile time
You can call
def f[T: TypeTag] = typeOf[T].members.filter(!_.isMethod)
f[Foo]
//Scope{
// private[this] val z: <?>;
// private[this] val b: <?>
//}
if you know type T statically i.e. at compile time (earlier).
You can call
def f_dyn(tpe: Type) = tpe.members.filter(!_.isMethod)
f_dyn(typeOf[Foo])
//Scope{
// private[this] val z: <?>;
// private[this] val b: <?>
//}
if you know type tpe dynamically i.e. at runtime (later).
You can express f via f_dyn
def f[T: TypeTag] = f_dyn(typeOf[T])
def f_dyn(tpe: Type) = tpe.members.filter(!_.isMethod)
If you want to iterate the method (apply it recursively) then it should return something like it accepts, i.e. now this is types rather than symbols, so you need to add somewhere something like .typeSignature, .asMethod.returnType, .asType.toType. Also maybe now you're more interested in .decls rather than .members since you are not looking for inherited members. Also .decls returns field symbols in correct order on contrary to .members. Finally let it be better List[...] rather than raw Scope (.toList)
def f[T: TypeTag]: List[Type] = f_dyn(typeOf[T])
def f_dyn(tpe: Type): List[Type] =
tpe.decls.filter(!_.isMethod).map(_.typeSignature).toList
f_dyn(f[Foo].head) // List(Int)
f_dyn(f_dyn(typeOf[Foo]).head) // List(Int)
You can iterate f_dyn
f_dyn(typeOf[Foo]) // List(Bar, Double)
f_dyn(typeOf[Foo]).map(f_dyn) // List(List(Int), List())
f_dyn(typeOf[Foo]).map(f_dyn).map(_.map(f_dyn)) // List(List(List()), List())
If you really want to iterate f rather than f_dyn then the complication is that you can call f[T] for the second time only on a statically known type T but you have the type that is the result of the first call only at runtime, you don't have it at compile time. In principle you can use runtime compilation (creating new compile time inside runtime) although this can work slower than ordinary reflection and doesn't seem needed now
import scala.reflect.runtime.{currentMirror => rm}
import scala.tools.reflect.ToolBox // libraryDependencies += scalaOrganization.value % "scala-compiler" % scalaVersion.value
val tb = rm.mkToolBox()
// suppose f is defined in object App
tb.eval(q"App.f[${f[Foo].head}]") // List(Int)
tb.eval(q"""
import App._
f[${f[Foo].head}]
""")
// List(Int)
Now all the classes Foo, Bar... are defined at compile time so it would make sense to use compile-time reflection (macros) rather than runtime reflection
Getting Case Class definition which points to another Case Class
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
def f[T]: List[String] = macro Macros.f_impl[T]
def f1[T]: List[List[String]] = macro Macros.f1_impl[T]
def f2[T]: List[List[List[String]]] = macro Macros.f2_impl[T]
class Macros(val c: blackbox.Context) {
import c.universe._
def f_dyn(tpe: Type): List[Type] =
tpe.decls.filter(!_.isMethod).map(_.typeSignature).toList
val ListObj = q"_root_.scala.List"
val ListT = tq"_root_.scala.List"
val StringT = tq"_root_.scala.Predef.String"
def f_impl[T: WeakTypeTag]: Tree = {
val types: List[Type] = f_dyn(weakTypeOf[T])
val typeStrings: List[String] = types.map(_.toString)
q"$ListObj.apply[$StringT](..$typeStrings)"
}
def f1_impl[T: WeakTypeTag]: Tree = {
val types: List[List[Type]] = f_dyn(weakTypeOf[T]).map(f_dyn)
val typeStrings: List[List[String]] = types.map(_.map(_.toString))
q"$ListObj.apply[$ListT[$StringT]](..$typeStrings)"
}
def f2_impl[T: WeakTypeTag]: Tree = {
val types: List[List[List[Type]]] =
f_dyn(weakTypeOf[T]).map(f_dyn).map(_.map(f_dyn))
val typeStrings: List[List[List[String]]] = types.map(_.map(_.map(_.toString)))
q"$ListObj.apply[$ListT[$ListT[$StringT]]](..$typeStrings)"
}
}
// in a different subproject
f[Foo]
//scalac: _root_.scala.List.apply[_root_.scala.Predef.String]("Bar", "Double")
f1[Foo]
//scalac: _root_.scala.List.apply[_root_.scala.List[_root_.scala.Predef.String]](scala.collection.immutable.List("Int"), scala.collection.immutable.List())
f2[Foo]
//scalac: _root_.scala.List.apply[_root_.scala.List[_root_.scala.List[_root_.scala.Predef.String]]](scala.collection.immutable.List(scala.collection.immutable.List()), scala.collection.immutable.List())
The runtime of macros (when they are expanded) is the compile time of main code.
Do macros support annotations too? like can I access my case class annotations with macros? with runtime reflection , i would do symbolOf[Foo].asClass.annotations
Yes, surely.
def foo[T]: Unit = macro fooImpl[T]
def fooImpl[T: c.WeakTypeTag](c: blackbox.Context): c.Tree = {
import c.universe._
println(symbolOf[T].asClass.annotations)
q"()"
}
class myAnnot extends StaticAnnotation
#myAnnot
case class Foo(b: Bar, z: Double)
symbolOf[Foo].asClass.annotations // at runtime: List(myAnnot)
foo[Foo]
// at compile time with scalacOptions += "-Ymacro-debug-lite":
// scalac: List(myAnnot)
One more option to perform compile-time calculations is to use one of libraries encapsulating work with macros e.g. Shapeless
// libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.10"
import shapeless.{::, DepFn0, DepFn1, HList, HNil, Generic, Poly0, Poly1, Typeable, poly}
trait DeepGeneric[T <: Product] {
type Repr <: HList
def to(t: T): Repr
def from(r: Repr): T
}
object DeepGeneric {
type Aux[T <: Product, Repr0 <: HList] = DeepGeneric[T] {type Repr = Repr0}
def instance[T <: Product, Repr0 <: HList](f: T => Repr0, g: Repr0 => T): Aux[T, Repr0] = new DeepGeneric[T] {
override type Repr = Repr0
override def to(t: T): Repr = f(t)
override def from(r: Repr): T = g(r)
}
implicit def deepGeneric[A <: Product, L <: HList, L1 <: HList](implicit
generic: Generic.Aux[A, L],
hListDeepGeneric: HListDeepGeneric.Aux[L, L1]
): Aux[A, L1] = instance(a => hListDeepGeneric.to(generic.to(a)), l1 => generic.from(hListDeepGeneric.from(l1)))
}
trait HListDeepGeneric[T <: HList] {
type Repr <: HList
def to(t: T): Repr
def from(r: Repr): T
}
trait LowPriorityHListDeepGeneric {
type Aux[T <: HList, Repr0 <: HList] = HListDeepGeneric[T] {type Repr = Repr0}
def instance[T <: HList, Repr0 <: HList](f: T => Repr0, g: Repr0 => T): Aux[T, Repr0] = new HListDeepGeneric[T] {
override type Repr = Repr0
override def to(t: T): Repr = f(t)
override def from(r: Repr): T = g(r)
}
implicit def headNotCaseClass[H, T <: HList, T_hListDeepGen <: HList](implicit
tailHListDeepGeneric: HListDeepGeneric.Aux[T, T_hListDeepGen]
): Aux[H :: T, H :: T_hListDeepGen] = instance({
case h :: t => h :: tailHListDeepGeneric.to(t)
}, {
case h :: t => h :: tailHListDeepGeneric.from(t)
})
}
object HListDeepGeneric extends LowPriorityHListDeepGeneric {
implicit val hNil: Aux[HNil, HNil] = instance(identity, identity)
implicit def headCaseClass[H <: Product, T <: HList, H_deepGen <: HList, T_hListDeepGen <: HList](implicit
headDeepGeneric: DeepGeneric.Aux[H, H_deepGen],
tailHListDeepGeneric: HListDeepGeneric.Aux[T, T_hListDeepGen]
): Aux[H :: T, H_deepGen :: T_hListDeepGen] = instance({
case h :: t => headDeepGeneric.to(h) :: tailHListDeepGeneric.to(t)
}, {
case h :: t => headDeepGeneric.from(h) :: tailHListDeepGeneric.from(t)
})
}
trait DeepMapper[P <: Poly1, In <: HList] extends DepFn1[In] {
type Out <: HList
}
trait LowPriorityDeepMapper {
type Aux[P <: Poly1, In <: HList, Out0 <: HList] = DeepMapper[P, In] {type Out = Out0}
def instance[P <: Poly1, In <: HList, Out0 <: HList](f: In => Out0): Aux[P, In, Out0] = new DeepMapper[P, In] {
override type Out = Out0
override def apply(t: In): Out = f(t)
}
implicit def headNotHList[P <: Poly1, H, T <: HList](implicit
headCase: poly.Case1[P, H],
tailDeepMapper: DeepMapper[P, T]
): Aux[P, H :: T, headCase.Result :: tailDeepMapper.Out] =
instance(l => headCase(l.head) :: tailDeepMapper(l.tail))
}
object DeepMapper extends LowPriorityDeepMapper {
implicit def hNil[P <: Poly1]: Aux[P, HNil, HNil] = instance(_ => HNil)
// implicit def headHList[P <: Poly1, H <: HList, H_deepMap <: HList, T <: HList](implicit
// headDeepMapper: DeepMapper.Aux[P, H, H_deepMap],
// headCase: poly.Case1[P, H_deepMap], // apply poly one more time
// tailDeepMapper: DeepMapper[P, T]
// ): Aux[P, H :: T, headCase.Result :: tailDeepMapper.Out] =
// instance(l => headCase(headDeepMapper(l.head)) :: tailDeepMapper(l.tail))
implicit def headHList[P <: Poly1, H <: HList, T <: HList](implicit
headDeepMapper: DeepMapper[P, H], // don't apply poly one more time
tailDeepMapper: DeepMapper[P, T]
): Aux[P, H :: T, headDeepMapper.Out :: tailDeepMapper.Out] =
instance(l => headDeepMapper(l.head) :: tailDeepMapper(l.tail))
}
trait DeepFillWith[P <: Poly0, L <: HList] extends DepFn0 {
type Out = L
}
trait LowPriorityDeepFillWith {
def apply[P <: Poly0, L <: HList](implicit deepFillWith: DeepFillWith[P, L]): DeepFillWith[P, L] = deepFillWith
def instance[P <: Poly0, L <: HList](f: => L): DeepFillWith[P, L] = new DeepFillWith[P, L] {
override def apply(): L = f
}
implicit def headNotHList[P <: Poly0, H, T <: HList](implicit
headCase: poly.Case0.Aux[P, H],
tailDeepFillWith: DeepFillWith[P, T]
): DeepFillWith[P, H :: T] =
instance(headCase() :: tailDeepFillWith())
}
object DeepFillWith extends LowPriorityDeepFillWith {
implicit def hNil[P <: Poly0]: DeepFillWith[P, HNil] = instance(HNil)
implicit def headHList[P <: Poly0, H <: HList, T <: HList](implicit
headDeepFillWith: DeepFillWith[P, H],
tailDeepFillWith: DeepFillWith[P, T]
): DeepFillWith[P, H :: T] =
instance(headDeepFillWith() :: tailDeepFillWith())
}
// needed if DeepMapper "applies poly one more time",
// e.g. for field NAMES and types (via DeepLabelledGeneric), not just types (via DeepGeneric)
// trait LowPriorityTypeablePoly extends Poly1 {
// implicit def notHListCase[V](implicit typeable: Typeable[V]): Case.Aux[V, String] =
// at(_ => typeable.describe)
// }
//
// object typeablePoly extends LowPriorityTypeablePoly {
// implicit def hListCase[V <: HList]: Case.Aux[V, V] = at(identity)
// }
object typeablePoly extends Poly1 {
implicit def cse[A](implicit typeable: Typeable[A]): Case.Aux[A, String] =
at(_ => typeable.describe)
}
object nullPoly extends Poly0 {
implicit def cse[A]: Case0[A] = at(null.asInstanceOf[A])
}
def classFieldTypes[T <: Product] = new PartiallyApplied[T]
class PartiallyApplied[T <: Product] {
def apply[L <: HList]()(implicit
deepGeneric: DeepGeneric.Aux[T, L],
deepFillWith: DeepFillWith[nullPoly.type, L],
deepMapper: DeepMapper[typeablePoly.type, L],
): deepMapper.Out = deepMapper(deepFillWith())
}
classFieldTypes[Bar]() // Int :: HNil
classFieldTypes[Foo]() // (Int :: HNil) :: Double :: HNil
Generic/LabelledGeneric/DeepGeneric, Mapper/DeepMapper, FillWith/DeepFillWith, Typeable are type classes.
lets say for each Type I want the code to behave differently, if Double do x, if Int do y.
You can use types comparisons t =:= typeOf[Double], t <:< typeOf[Double] if you use runtime/compile-time reflection or you can keep using type classes and polymorphic functions
trait MyTypeclass[T] {
def apply(): Unit
}
object MyTypeclass {
implicit val double: MyTypeclass[Double] = () => println("do x")
implicit val int: MyTypeclass[Int] = () => println("do y")
implicit def caseClass[T <: Product, L <: HList](implicit
deepGeneric: DeepGeneric.Aux[T, L],
deepFillWith: DeepFillWith[nullPoly.type, L],
deepMapper: DeepMapper[myPoly.type, L]
): MyTypeclass[T] = () => deepMapper(deepFillWith())
}
object myPoly extends Poly1 {
implicit def cse[T: MyTypeclass]: Case.Aux[T, Unit] = at(_ => foo)
}
def foo[T](implicit tc: MyTypeclass[T]): Unit = tc()
foo[Int]
// do y
foo[Double]
// do x
foo[Foo]
// do y
// do x
foo[Bar]
// do y
Shapeless is also capable of handling annotations
import shapeless.Annotation
implicitly[Annotation[myAnnot, Foo]].apply() // myAnnot#1a3869f4

Reverse HList and convert to class?

I'm using Shapeless to accumulate materialized values in Akka as an HList and convert that to a case class.
(You don't have to know Akka much for this question, but the default approach accumulates materialized values as recursively nested 2-tuples, which isn't much fun, so Shapeless HLists seemed a more sensible approach -- and works pretty well. But I don't know how to properly re-use that approach. Here, I'll simplify the kinds of values Akka produces.)
For example, let's say we've got two materialized types, "A" and "B":
case class Result(b: B, a: A)
createA
.mapMaterialized((a: A) => a :: HNil)
.viaMat(flowCreatingB)((list1, b: B) => b :: list1)
.mapMaterialized(list2 => Generic[Result].from(list2))
// list1 = A :: HNil
// list2 = B :: A :: HNil
... and that produces Result just fine. But it requires that your case class be written backwards -- first value last, etc -- which is kind of dorky and hard to follow.
So the sensible thing is to reverse the list before converting to the case class, like this:
case class Result(a: A, b: B)
// ...
.mapMaterialized(list2 => Generic[Result].from(list2.reverse))
Now we can think about Result properties in the same order they were built. Yay.
But how to simplify and reuse this line of code?
The problem is that implicits don't work on multiple type parameters. For example:
def toCaseClass[A, R <: HList](implicit g: Generic.Aux[A, R], r: Reverse.Aux[L, R]): R => A =
l => g.from(l.reverse)
I'd need to specify both A (Result, above) and the HList being built:
.mapMaterialized(toCaseClass[Result, B :: A :: HNil])
Obviously, that invocation is going to be absurd with long lists (and Akka tends to build up really ugly-looking materialized types, not merely "A" and "B"). It'd be nicer to write something like:
.mapMaterialized(toCaseClass[Result])
I've tried to solve this using implicits, like this:
implicit class GraphOps[Mat <: HList](g: RunnableGraph[Mat]) {
implicit def createConverter[A, RL <: HList](implicit
r: Reverse.Aux[Mat, RL],
gen: Generic.Aux[A, RL]): Lazy[Mat => A] =
Lazy { l =>
val x: RL = l.reverse
val y: A = gen.from(x)
gen.from(l.reverse)
}
def toCaseClass[A](implicit convert: Lazy[Mat => A]): RunnableGraph[A] = {
g.mapMaterializedValue(convert.value)
}
But the compiler complains "No implicit view available".
The deeper problem is that I don't quite understand how to properly infer...
// R = Reversed order (e.g. B :: A :: NHNil)
// T = Type to create (e.g. Result(a, b))
// H = HList of T (e.g. A :: B :: HNil)
gen: Generic.Aux[T, H] // Generic[T] { type Repr = H }
rev: Reverse.Aux[R, H] // Reverse[R] { type Out = H }
This is sort of backwards from how Shapeless likes to infer things; I can't quite chain the abstract type members properly.
Profound thanks if you have insight here.
My bad: the example above, of course, requires Akka to compile. A simpler way of putting it is this (with thanks to Dymtro):
import shapeless._
import shapeless.ops.hlist.Reverse
case class Result(one: String, two: Int)
val results = 2 :: "one" :: HNil
println(Generic[Result].from(results.reverse))
// this works: prints "Result(one,2)"
case class Converter[A, B](value: A => B)
implicit class Ops[L <: HList](list: L) {
implicit def createConverter[A, RL <: HList](implicit
r: Reverse.Aux[L, RL],
gen: Generic.Aux[A, RL]): Converter[L, A] =
Converter(l => gen.from(l.reverse))
def toClass[A](implicit converter: Converter[L, A]): A =
converter.value(list)
}
println(results.toClass[Result])
// error: could not find implicit value for parameter converter:
// Converter[Int :: String :: shapeless.HNil,Result]
Dymtro's final example, below...
implicit class GraphOps[Mat <: HList, R <: HList](g: RunnableGraph[Mat]) {
def toCaseClass[A](implicit
r: Reverse.Aux[Mat, R],
gen: Generic.Aux[A, R]
): RunnableGraph[A] = g.mapMaterializedValue(l => gen.from(l.reverse))
}
... does seem to do what I'd been hoping for. Thank you very much Dmytro!
(Note: I had been somewhat misled in analyzing it earlier: it seems IntelliJ's presentation compiler incorrectly insists it won't compile (missing implicits). Moral: Don't trust IJ's presentation compiler.)
If I understood correctly you wish that in
def toCaseClass[A, R <: HList, L <: HList](implicit
g: Generic.Aux[A, R],
r: Reverse.Aux[L, R]
): L => A = l => g.from(l.reverse)
you could specify only A and then R, L be inferred.
You can do this with PartiallyApplied pattern
import shapeless.ops.hlist.Reverse
import shapeless.{Generic, HList, HNil}
def toCaseClass[A] = new {
def apply[R <: HList, L <: HList]()(implicit
g: Generic.Aux[A, R],
r0: Reverse.Aux[R, L],
r: Reverse.Aux[L, R]
): L => A = l => g.from(l.reverse)
}
class A
class B
val a = new A
val b = new B
case class Result(a: A, b: B)
toCaseClass[Result]().apply(b :: a :: HNil)
(without implicit r0 type parameter L can't be inferred upon call of .apply() because L becomes known only upon call .apply().apply(...))
or better
def toCaseClass[A] = new {
def apply[R <: HList, L <: HList](l: L)(implicit
g: Generic.Aux[A, R],
r: Reverse.Aux[L, R]
): A = g.from(l.reverse)
}
toCaseClass[Result](b :: a :: HNil)
(here we don't need r0 because L becomes known already upon call .apply(...)).
If you want you can replace anonymous class with named one
def toCaseClass[A] = new PartiallyApplied[A]
class PartiallyApplied[A] {
def apply...
}
Alternatively you can define a type class (although this is a little more wordy)
trait ToCaseClass[A] {
type L
def toCaseClass(l: L): A
}
object ToCaseClass {
type Aux[A, L0] = ToCaseClass[A] { type L = L0 }
def instance[A, L0](f: L0 => A): Aux[A, L0] = new ToCaseClass[A] {
type L = L0
override def toCaseClass(l: L0): A = f(l)
}
implicit def mkToCaseClass[A, R <: HList, L <: HList](implicit
g: Generic.Aux[A, R],
r0: Reverse.Aux[R, L],
r: Reverse.Aux[L, R]
): Aux[A, L] = instance(l => g.from(l.reverse))
}
def toCaseClass[A](implicit tcc: ToCaseClass[A]): tcc.L => A = tcc.toCaseClass
toCaseClass[Result].apply(b :: a :: HNil)
Hiding several implicits with a type class: How to wrap a method having implicits with another method in Scala?
You could find an answer to your question in Type Astronaut:
https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:ops:migration (6.3 Case study: case class migrations)
Notice that IceCreamV1("Sundae", 1, true).migrateTo[IceCreamV2a] takes a single type parameter.
Your code with GraphOps doesn't work for several reasons.
Firstly, shapeless.Lazy is not just a wrapper. It's a macro-based type class to handle "diverging implicit expansion" (in Scala 2.13 there are by-name => implicits for that, although they are not equivalent to Lazy). You should use Lazy when you understand why you need it.
Secondly, you seem to define some implicit conversion (implicit view, Mat => A) but resolution of implicit conversions is trickier than resolution of other implicits (1 2 3 4 5).
Thirdly, you seem to assume that when you define
implicit def foo: Foo = ???
def useImplicitFoo(implicit foo1: Foo) = ???
foo1 is foo. But generally this is not true. foo is defined in current scope and foo1 will be resolved in the scope of useImplicitFoo call site:
Setting abstract type based on typeclass
When doing implicit resolution with type parameters, why does val placement matter? (difference between implicit x: X and implicitly[X])
So implicit createConverter is just not in scope when you call toCaseClass.
Fixed version of your code is
trait RunnableGraph[Mat]{
def mapMaterializedValue[A](a: Mat => A): RunnableGraph[A]
}
case class Wrapper[A, B](value: A => B)
implicit class GraphOps[Mat <: HList](g: RunnableGraph[Mat]) {
val ops = this
implicit def createConverter[A, RL <: HList](implicit
r: Reverse.Aux[Mat, RL],
gen: Generic.Aux[A, RL],
): Wrapper[Mat, A] =
Wrapper { l =>
val x: RL = l.reverse
val y: A = gen.from(x)
gen.from(l.reverse)
}
def toCaseClass[A](implicit convert: Wrapper[Mat, A]): RunnableGraph[A] = {
g.mapMaterializedValue(convert.value)
}
}
val g: RunnableGraph[B :: A :: HNil] = ???
val ops = g.ops
import ops._
g.toCaseClass[Result]
Try
import akka.stream.scaladsl.RunnableGraph
import shapeless.{::, Generic, HList, HNil}
import shapeless.ops.hlist.Reverse
implicit class GraphOps[Mat <: HList, R <: HList](g: RunnableGraph[Mat]) {
def toCaseClass[A](implicit
r: Reverse.Aux[Mat, R],
gen: Generic.Aux[A, R]
): RunnableGraph[A] = g.mapMaterializedValue(l => gen.from(l.reverse))
}
case class Result(one: String, two: Int)
val g: RunnableGraph[Int :: String :: HNil] = ???
g.toCaseClass[Result]

Poly fold return type not inferred correctly

I am trying to create a poly function that folds over a tuple of Foos:
case class Foo[A](a: A)
object extractFold extends Poly2 {
implicit def default[A, As <: HList]: Case.Aux[Foo[A], Foo[As], Foo[A :: As]] = {
???
}
}
def extract[In, A <: HList, B <: HList](keys: In)
(implicit
gen: Generic.Aux[In, A],
folder: RightFolder.Aux[A, Foo[HNil], extractFold.type, Foo[B]],
tupler: Tupler[B])
: Foo[tupler.Out] = {
???
}
val result = extract((Foo(1), Foo("a")))
The function works at runtime, but the compiler inferred result type is always Foo[Unit] which is not right - in this example it should be Foo[(Int, String)]
Why do you think that
the compiler inferred result type is always Foo[Unit]
?
The following code
import shapeless.ops.hlist.{RightFolder, Tupler}
import shapeless.{::, Generic, HList, HNil, Poly2}
import scala.reflect.runtime.universe.{typeOf, Type, TypeTag}
object App {
def getType[T: TypeTag](t: T): Type = typeOf[T]
case class Foo[A](a: A)
object extractFold extends Poly2 {
implicit def default[A, As <: HList]: Case.Aux[Foo[A], Foo[As], Foo[A :: As]] =
at { case (Foo(a), Foo(as)) => Foo(a :: as) }
}
def extract[In, A <: HList, B <: HList](keys: In)(implicit
gen: Generic.Aux[In, A],
folder: RightFolder.Aux[A, Foo[HNil], extractFold.type, Foo[B]],
tupler: Tupler[B]
): Foo[tupler.Out] =
Foo(tupler(folder(gen.to(keys), Foo(HNil)).a))
val result = extract((Foo(1), Foo("a")))
def main(args: Array[String]): Unit = {
println(
getType(result)
)
}
}
prints
App.Foo[(Int, java.lang.String)]
Moreover, if you change the line
val result: Foo[Unit] = extract((Foo(1), Foo("a")))
the code doesn't compile.
By the way, the same can be done with
import shapeless.PolyDefns.~>
import shapeless.ops.hlist.{Comapped, NatTRel, Tupler}
import shapeless.{Generic, HList, Id}
object App {
case class Foo[A](a: A)
def extract[In, A <: HList, B <: HList](keys: In)(implicit
gen: Generic.Aux[In, A],
comapped: Comapped.Aux[A, Foo, B],
natTRel: NatTRel[A, Foo, B, Id],
tupler: Tupler[B]
): Foo[tupler.Out] =
Foo(tupler(natTRel.map(new (Foo ~> Id) { def apply[T](foo: Foo[T]) = foo.a }, gen.to(keys))))
val result = extract((Foo(1), Foo("a")))
def main(args: Array[String]): Unit = {
println(result)//Foo((1,a))
}
}
Maybe someone with a better understanding of shapeless can provide you a better answer. According to my understanding the problem lies at the type inference step. If you specify all the types explicitly as in
val result: Foo[(Int, String)] = extract[(Foo[Int], Foo[String]),
Foo[Int] :: Foo[String] :: HNil,
Int :: String :: HNil]((Foo(1), Foo("a")))
the code correctly typechecks. Obviously you don't want to specify those types explicitly though.
According to my understanding the compiler can't infer good B and tupler.Out because they are not coupled tight enough to In and A. One way you can work this around is by introducing an intermediate trait like this:
trait Extractor[L <: HList, HF] {
type FR <: HList
type TR
val folder: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR]]
val tupler: Tupler.Aux[FR, TR]
}
object Extractor {
type Aux[L <: HList, HF, FR0 <: HList, TR0] = Extractor[L, HF] {type FR = FR0; type TR = TR0}
implicit def wrap[L <: HList, In, HF, FR0 <: HList, TR0](implicit folder0: RightFolder.Aux[L, Foo[HNil], HF, Foo[FR0]],
tupler0: Tupler.Aux[FR0, TR0]) = new Extractor[L, HF] {
type FR = FR0
type TR = TR0
override val folder = folder0
override val tupler = tupler0
}
}
and then using it like this:
def extract[In, A <: HList, B <: HList, C](keys: In)
(implicit gen: Generic.Aux[In, A],
extractor: Extractor.Aux[A, extractFold.type, B, C])
: Foo[C] = {
val hli = gen.to(keys)
val fr = extractor.folder(hli, Foo(HNil))
Foo(extractor.tupler(fr.a))
}
This is a hacky solution but at least it seem to work (see also online demo).

How do I best construct a shapeless record with a default value?

Say I have a shapeless record:
trait T
case object Ti extends T
case object Ta extends T
case object To extends T
type R = Record.`Ta -> Option[String], Ti -> Option[Int]`.T
val r: R = (Ta ->> Option("plif")) :: (Ti ->> Option(4)) :: HNil
I'd like to write a function get such that:
get(Ta) == Some("plif")
get(Ti) == Some(4)
get(To) == None
What would be the best way to achieve this?
A simple solution is to provide your own Selector instance for a default case:
class DefaultSelector[R <: HList, K] extends Selector[R, K] {
type Out = Option[Nothing]
def apply(l: R): Out = None
}
def get[K, V](k: K)(
implicit sel: Selector[R, K] = new DefaultSelector[R, K]
): sel.Out = sel(r)
But with that code Scala's compiler may have difficulties providing TypeTags for the result of the default case.
So to fix that you can also write a new typeclass DefaultSelector, which will default to None: Option[Nothing], if no Selector is found:
import shapeless._
import shapeless.ops.record._
trait DefaultSelector[R <: HList, K] {
type Out
def apply(r: R): Out
}
sealed trait LowPriorityDefaultSelector {
type Aux[R <: HList, K, V] = DefaultSelector[R, K] { type Out = V }
case class Impl[R <: HList, K, V](get: R => V) extends DefaultSelector[R, K] {
type Out = V
def apply(r: R): Out = get(r)
}
implicit def default[R <: HList, K, V](
implicit ev: Option[Nothing] =:= V // tricking Scala's implicit resolution
): Aux[R, K, V] =
Impl[R, K, V](Function.const(None))
}
object DefaultSelector extends LowPriorityDefaultSelector {
implicit def existing[R <: HList, K, V](
implicit sel: Selector.Aux[R, K, V]
): Aux[R, K, V] =
Impl[R, K, V](sel.apply)
}
Then the get function becomes:
def get[K, V](k: K)(implicit sel: DefaultSelector[R, K]): sel.Out = sel(r)
And the result (for both solutions) is:
scala> get(Ti)
res0: Option[Int] = Some(4)
scala> get(Ta)
res1: Option[String] = Some(plif)
scala> get(To)
res2: Option[Nothing] = None

use implicit to find "One" element of HList typeclass to inject

Here is the code:
trait Service[T<: HList] {
def doStuff(): Unit
}
class A
class B
class C
class ServiceAB extends Service[A :: B :: HNil] {
override def doStuff(): Unit = println("handling a b")
}
class ServiceC extends Service[C :: HNil] {
override def doStuff(): Unit = println("handling c")
}
implicit val serviceAB = new ServiceAB
implicit val serviceC = new ServiceC
def operate[T, W <: HList](x: T)(implicit service: Service[W]) = {
service.doStuff()
}
operate(new C)
I just wonder is it possible or what should I code in the type level to inject implicit serviceC when operate(new C) is executed, since class C is the element of HList of type class of ServiceC ?
Many thanks in advance
I realy don't know why you need this :)
So your code works, but if you pass type parameter explicitly:
operate[C, C :: HNil](new C)
If you want same, but implicitly, you can define your class type:
trait Service[L <: HList, U] { def doStuff(): Unit }
trait lowPriority {
implicit def otherwise[L <: HList, U] =
new Service[L, U] {
def doStuff(): Unit = println("handling otherwise")
}
}
object Service extends lowPriority {
implicit def ab[L <: HList, U]
(implicit e: L =:= (A :: B :: HNil),
s: Selector[L, U]) =
new Service[L, U] {
def doStuff(): Unit = println("handling a b")
}
implicit def c[L <: HList, U]
(implicit e: L =:= (C :: HNil),
s: Selector[L, U]) =
new Service[L, U] {
def doStuff(): Unit = println("handling c")
}
}
}
def operate[T, W <: HList](x: T)(implicit service: Service[W, T]) = {
service.doStuff()
}
So this works as expected:
operate(new C) //> handling c
operate(new A) //> handling a b
operate(new B) //> handling a b
It is possible to make it more general (so it will check is type you need is in a HList, ohterwise if it doesn't) (using Curry-Howard isomorphism, great article with explanations by Miles Sabin: http://www.chuusai.com/2011/06/09/scala-union-types-curry-howard/):
import reflect.runtime.universe._
type ¬[A] = A => Nothing
type ∨[T, U] = ¬[¬[T] with ¬[U]]
type ¬¬[A] = ¬[¬[A]]
class A
class B
class C
class D //> additional class for example
trait Service[L <: HList, U] { def doStuff(): Unit }
trait lowPriority {
implicit def otherwise[L <: HList, U] =
new Service[L, U] {
def doStuff(): Unit = println("handling otherwise")
}
}
object Service extends lowPriority {
implicit def ab[L <: HList, U]
(implicit e: (¬¬[U] <:< (A ∨ B)),
s: Selector[L, TypeTag[U]]) =
new Service[L, U] {
def doStuff(): Unit = println("handling a b")
}
implicit def c[L <: HList, U](implicit e: U =:= C, s: Selector[L, TypeTag[U]]) =
new Service[L, U] {
def doStuff(): Unit = println("handling c")
}
}
}
def operateBi[T, W <: HList](x: T, w: W)(implicit service: Service[W, T]) = {
service.doStuff()
}
Defining HLists of types:
val hl1 = implicitly[TypeTag[A]] :: implicitly[TypeTag[B]] :: HNil
val hl2 = implicitly[TypeTag[C]] :: HNil
operateBi(new C, hl1)
operateBi(new A, hl2)
operateBi(new B, hl1)
operateBi(new D, hl1)
Works as expected.