shapeless: Generic programming for Scala | Latest stable release 2.3.3 | Code of conduct https://www.scala-lang.org/conduct/
milessabin on master
Fix Typeable.describe for symbo… Merge pull request #945 from jo… (compare)
final case class Person(name: Option[String], age: Option[Int])
def foo[R <: HList, V <: HList: UnaryTCConstraint.*->*[Option]#λ](
person: Person
)(implicit gen: LabelledGeneric.Aux[Person, R], values: Values.Aux[R, V]) = {
val v: V = values(gen.to(person))
object combine extends Poly2 {
implicit def default[Acc <: HList, A] = at { (acc: Acc, r: Option[A]) =>
r match {
case None => acc
case Some(value) => value :: acc
}
}
}
v.foldLeft(HNil)(combine)
}
HList
of Option
s, then adding the machinery to get that HList
of Option
s from a case class
via LabelledGeneric
.
Person
is Option[String] :: Option[Int] :: HNil
String :: HNil
or Int :: HNil
or String :: Int :: HNil
depending on whether the Option
is Some
or None
, so based on the class of those values, not their types
Person(None, None, None...)
mean?
None
, I'd suggest a case class
with all fields Option
s isn't the appropriate type.
Thing[F[_]](a: F[String], b: F[Int])
kittens
from Thing[Option]
to Option[Thing[Id]]
Hello,
I'm trying to develop a library to read case classes from environment variables.
Given the case class Foo(num: Int)
and the env val NUM=42
the following method returns:
def readFromEnv[A](implicit envReader: EnvReader[A]): envReader.Out = envReader.readEnv
readFromEnv[Foo]
42 :: HNil
Now in order to turn this HList back to its original case class I come up with the following:
def readFromEnv2[A, O](implicit envReader: EnvReader.Aux[A, O], gen: Generic.Aux[A, O]): A = gen.from(readFromEnv[A])
Here I needed to introduce the second type parameter O, in order to align the proper output types. This compiles but at the use site it requires to specify both type parameters. Is there a way to specify only the A parameter (the case class) on the use site?
I'm trying to perform an operation on two coproduct values, is Poly2 an appropriate type for this?
something like this is fine
object multiply extends Poly2 {
implicit val intIntCase: Case.Aux[Int, Int, Int] =
at((a, b) => a * b)
implicit val intStrCase: Case.Aux[Int, String, Int] =
at((a, b) => b.length * a)
}
multiply(3, "4")
but I'd like to be able to use it on coproducts
type A = Int :+: String :+: CNil
multiply(Coproduct[A](3) , Coproduct[A]("4"))
but this doesn't compile
could not find implicit value for parameter cse: shapeless.poly.Case[Playground.this.multiply.type,Playground.this.A :: Playground.this.A :: shapeless.HNil]
Poly
s like that, but I'm sure it's doable.
A
case that selects the appropriate case
Hi! I'm new to Shapeless and I have several questions and this piece of code, it's not really complex.
I'd be grateful if anyone would check it: https://scastie.scala-lang.org/pedorich-n/9ieO4UUDQ3yjQHNQG9KYfA/3.
So I have here an ADT named FileType
, and holder FileEntry[T <: FileType]
and some set of operations for these FileEntries.
I wonder if it's possible to make it more generic and let the compiler do all the job of matching Operations to FileTypes? Is it possible to derive FileEntryAny
from AnyType
, like Generic[FileEntry[AnyType]]
, so when I add new FileType I don't have to add it to FileEntryAny
?
I feel like it's all possible, by using some Selector, Inject, Witness instances, but I can't really wrap my head about how to design this.
Thanks!