Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 29 20:39

    joroKr21 on main

    Update sbt-scalajs, scalajs-com… (compare)

  • Jan 29 20:39
    joroKr21 closed #1297
  • Jan 29 20:31
    joroKr21 synchronize #1297
  • Jan 29 20:30

    joroKr21 on main

    Update scala-parser-combinators… (compare)

  • Jan 29 20:30
    joroKr21 closed #1298
  • Jan 29 19:41
    joroKr21 synchronize #1298
  • Jan 29 19:41

    joroKr21 on main

    Update junit-plugin, junit-runt… (compare)

  • Jan 29 19:41
    joroKr21 closed #1299
  • Jan 29 19:14
    scala-steward opened #1299
  • Jan 27 20:05
    scala-steward opened #1298
  • Jan 27 20:05
    scala-steward opened #1297
  • Jan 07 17:55

    joroKr21 on main

    Update sbt to 1.8.2 (#1296) (compare)

  • Jan 07 17:55
    joroKr21 closed #1296
  • Jan 07 00:30
    DmytroMitin commented #1285
  • Jan 05 17:36
    scala-steward opened #1296
  • Dec 28 2022 14:35

    joroKr21 on main

    Fixed typo (#1295) (compare)

  • Dec 28 2022 14:35
    joroKr21 closed #1295
  • Dec 28 2022 07:13
    windymelt opened #1295
  • Dec 15 2022 20:59

    joroKr21 on main

    Update slf4j-nop to 2.0.6 (#129… (compare)

  • Dec 15 2022 20:59
    joroKr21 closed #1294
Dmitry Avershin
@dmitraver_twitter

hi guys, I have encountered a very interesting issue which I can't really explain but maybe somebody here faced something similar. We use Scanamo in our web application for DynamoDB access. Scanamo uses shapeless internally for conversion between case classes and dynamodb data model. Basically, for this to work there should be implicit formatter in scope for the case class, these formatters are automatically derived via shapeless. I once added a new field to my case class and noticed that tests started blocking infinitely. After hours spent debugging I found out that the place that actually hangs forever is the call to LabelledGeneric.from during the derivation phase but when I tried to evaluate this expression in Intellij debugger it showed me that there is an exception

java.lang.NoSuchMethodError: somepackage.MyCaseClass.<init>(Lscala/Option;Lscala/Option;Ljava/lang/Double;)V

what is interesting here is that this list of parameters doesn't include my new field even though its in code. What is even more interesting is that when I tried to summon the implicit for the formatter on the very high level in my application code like implicitly[DynamoFormat[MyCaseClass]] all code started working again without any blockage and even after I removed this line. When I added new fields again in future I had no problems with it but my colleagues had the same issue. To me it looks like shapeless somehow loads the old (cached somewhere?) version of case class in the situation where code blocks but why then the exception is not thrown? Has anybody seen something similar? Thanks in advance.

Thoren Johne
@thoren
looks like Shapeless is somehow addicted to a clean compile, wish it would be more handsome at that point.
ritschwumm
@ritschwumm
hm, can this be related to sbt/zinc#249 ?
elyphas
@elyphas

Hi, i am reading the book "the type astronaut's guide to shapeless" and this has written "types are too specifc", and I have a doubt, "generic" is it the same that "dynamic programming", or what would be the difference?

someone could explain this to me?, please

elyphas
@elyphas
And basically because i understand that type class is better than generic like in "standard template libraries" in c++
elyphas
@elyphas
@DmytroMitin , thank you, :) sorry
samthebest
@samthebest
Trying to automagically create case classes from Map[String, Any]
Dmytro Mitin
@DmytroMitin
@samthebest As I commented in your question at SO, for List you should define one more instance of type class. You can use instance I wrote for Option as an example. If you want this approach to work with any structures like Option, List, nested sealed traits + case classes then probably instances should be defined for arbitrary products/coproducts.
dragonfly.ai
@dragonfly-ai

Could Shapeless help to eliminate the following boiler plate?

Please consider a trait called Navigable. Any class that extends this trait must implement a method that takes a path as a parameter and returns the value at the end of the path. The path encodes steps through the object graph reachable from this object. For example, the path: "foo/8/colors/red" might correspond to this.foo(8).colors.get("red"); that is, a dot access, an array access, then a map retrieval.

We can assume constraints on Navigable objects: 1. They always consist of directed acyclic object graphs. 2. They only reference primitives, maps, arrays, and other Navigable objects.

Can shapeless eliminate the need to write path comprehension code for every single Navigable type?

Fabio Labella
@SystemFw
@dragonfly-ai you should look into lenses, like the Monocle library
dragonfly.ai
@dragonfly-ai

@SystemFw Thanks for the suggestion. I have looked into lenses and the Monocle library. If you write code to declare how they compose with each other, Lenses simulate object mutations by simplifying the process of creating copies of all of the nodes in an object graph.

How does that relate to parsing a path of the form: "foo/8/colors/red" into a series of field, array, and map accesses?

Fabio Labella
@SystemFw
because you can parse that path into an optic (not just lenses, but also traversals and so on)
samthebest
@samthebest

@DmytroMitin

You can use instance I wrote for Option as an example.

Ah OK I'll give it a whirl. Thanks for all the help!

dragonfly.ai
@dragonfly-ai
@SystemFw wouldn't I have to write Monocle code for every class that extends Navigable? Is there a Scala.js friendly way to generate that code or functionality automatically without configuration or special code?
Fabio Labella
@SystemFw
I think Monocle comes with several ways of avoiding the boilerplate. The goggles library might also be useful for that
samthebest
@samthebest

@DmytroMitin So I'm trying

implicit def hconsFromMap0list[K <: Symbol, V, R <: HList, T <: HList](implicit
                                                                        witness: Witness.Aux[K],
                                                                        gen: LabelledGeneric.Aux[V, R],
                                                                        fromMapH: FromMap[R],
                                                                        fromMapT: FromMap[T]
                                                                       ): FromMap[FieldType[K, List[V]] :: T] =
    new FromMap[FieldType[K, List[V]] :: T] {
      def apply(m: Map[String, Any]): Option[FieldType[K, List[V]] :: T] = (for {
        v <- m.get(witness.value.name)
        r <- Typeable[Map[String, Any]].cast(v)
        h <- fromMapH(r)
        t <- fromMapT(m)
      } yield field[K](List(gen.from(h))) :: t).orElse(for {
        v <- m.get(witness.value.name)
        r1 <- Typeable[List[Map[String, Any]]].cast(v)
        opt = for {
          r <- r1
          h <- fromMapH(r)
        } yield gen.from(h)
        t <- fromMapT(m)
      } yield field[K](opt) :: t)
    }

But the following test fails with NSEE None.get

case class NestedFoo(foos: Option[List[Foo]])
case class Foo(foo: String)

"NestedFoo" in {
      val map = Map[String, Any]("foos" -> Some(List(Map("foo" -> "value"), Map("foo" -> "value2"))))

      CaseClassFromMap[NestedFoo](map) must_=== NestedFoo(foos = Some(List(
        Foo(
          foo = "value"
        ),
        Foo(
          foo = "value2"
        )
      )))
    }
Dmytro Mitin
@DmytroMitin
@samthebest Well, you wrote an instance for List. So it works for case class NestedFoo(foos: List[Foo])
CaseClassFromMap[NestedFoo](Map("foos" -> List(Map("foo" -> "value"), Map("foo" -> "value2")))) \\ NestedFoo(List(Foo(value), Foo(value2)))
CaseClassFromMap[NestedFoo](Map("foos" -> Map("foo" -> "value"))) \\ NestedFoo(List(Foo(value)))
Why do you expect it to work automatically for Option[List[..]]? There's no magic here. Type class works for types for which you defined its instances.
Philipp Martini
@maphi

Hi is there a way to define an untag method like this:

implicit class Untagger[T, U](val a: T @@ U) extends AnyVal {
  def untag: T = a
}

Currently scala unfortunately infers the type with it's Tag as T

(using 2.11)
Philipp Martini
@maphi
Ok finally found a solution. Adding T as second type parameter to tag.Tagged makes this work.
samthebest
@samthebest

@DmytroMitin I guess what I'm hoping for is some magic since there are infinite number of combinations of List[List or List[Option[Option or List[List[Option etc. If there was a way to combine the type-class for List automagically with Option for any combination of nestings that would be cool.

But in case that request is impossible, it would be nice if you could help me figure out how to just get one level of nesting working. I'm a bit stuck on Option[List[. This is what I have got so far

implicit def hconsFromMap0optList[K <: Symbol, V, R <: HList, T <: HList](implicit
                                                                         witness: Witness.Aux[K],
                                                                         gen: LabelledGeneric.Aux[V, R],
                                                                         fromMapH: FromMap[R],
                                                                         fromMapT: FromMap[T]
                                                                        ): FromMap[FieldType[K, Option[List[V]]] :: T] =
    new FromMap[FieldType[K, Option[List[V]]] :: T] {
      def apply(m: Map[String, Any]): Option[FieldType[K, Option[List[V]]] :: T] = (for {
        v <- m.get(witness.value.name)
        r <- Typeable[Map[String, Any]].cast(v)
        h <- fromMapH(r)
        t <- fromMapT(m)
      } yield field[K](Some(List(gen.from(h)))) :: t).orElse(for {
        v <- m.get(witness.value.name)
        r1 <- Typeable[Option[List[Map[String, Any]]]].cast(v)
        opt = for {
          r <- r1
          h <- fromMapH(r)
          list = for {
            r2 <- r
            h2 <- fromMapH(r2)
          } yield gen.from(h2)
        } yield ???
        t <- fromMapT(m)
      } yield field[K](opt) :: t)
    }

As you can see I'm a bit confused on how to get the for-comprehensions to work with this extra level of nesting.

Again appreciate the help! I'm very new to Shapeless.

Billzabob
@Billzabob
Is it possible to have an HList type where every element must be a tuple? I'd like to have my method take in an HList of pairs.
Dmytro Mitin
@DmytroMitin
@Billzabob Sounds like a type class
 def foo[L <: HList](implicit m: AllPairs[L]) = ???

 trait AllPairs[L <: HList]
 object AllPairs {
   implicit val hnil: AllPairs[HNil] = null
   implicit def hcons[A, B, T <: HList](implicit tail: AllPairs[T]): AllPairs[(A, B) :: T] = null
 }
Dmytro Mitin
@DmytroMitin
@Billzabob Actually it's just standard shapeless.ops.hlist.LiftAll
  type IsTuple2[A] = A <:< Tuple2[_, _]
  def foo[L <: HList](l: L)(implicit m: LiftAll[IsTuple2, L]) = ???
Dmytro Mitin
@DmytroMitin
@samthebest I guess list should be instead of ??? (remove the line h <- fromMapH(r)that doesn't compile).
Billzabob
@Billzabob
@DmytroMitin Thanks. I think I'm a little too new to shapeless to figure out where to go from there though. Something with m.instances I'm assuming? Can you point me somewhere where I can learn more? Is there documentation that would help? Googling shapless LiftAll didn't get me much. All I've ever done with shapeless is basic HList stuff.
Ethan
@esuntag
I've found the Type Astronaut's Guide to Shapeless to be a great intro: https://underscore.io/books/shapeless-guide/
After going through that, you should hopefully have enough knowledge to look through the shapeless.ops files in the git repo.
Dmytro Mitin
@DmytroMitin
@Billzabob In this particular case you can treat LiftAll as a black box. Just foo((1, "a") :: (true, 1.0) :: HNil)compiles and foo((1, "a") :: (true, 1.0) :: 1L :: HNil) doesn't.
Billzabob
@Billzabob
@DmytroMitin What about inside of foo though? Because it still just takes a plain old HList. So I'm guessing I need to use that implicit m value inside to get the proper types out, correct?
Dmytro Mitin
@DmytroMitin
@Billzabob Inside foo should be your logic.
Billzabob
@Billzabob
@DmytroMitin Ok cool. It seemed like it was inferring the wrong types (like Any) when I would match, but I haven't tested it yet so it could just be my IDE getting confused.
samthebest
@samthebest

@DmytroMitin Thanks, I'll give that a go.

FYI I've worked out a way to do all this with Circe. It seems to require much less boiler plate since it handles all the possible combinations of nested Option and List. https://stackoverflow.com/a/55355685/1586965

mert
@inanme

Hi all,
Hopefully this is the right place to ask this question, and also right way of asking a question.

I have this tagging mechanism.

object Validation {
  import shapeless.tag
  import shapeless.tag._
  type ContentId = String @@ ValidContentId
  object ValidContentId1 {
    def apply(contentId: String): ContentId = tag[ValidContentId][String](contentId)
  }
}

I used akka-http with spray json

This compiles

    val list = List(Validation.ValidContentId1("c1"), Validation.ValidContentId1("c2"))
    complete(list.asInstanceOf[List[String]])

This does not

    val list = List(Validation.ValidContentId1("c1"), Validation.ValidContentId1("c2"))
    complete(list)

The error is

[error]  found   : List[watchlist.Validation.ContentId]
[error]     (which expands to)  List[String with shapeless.tag.Tagged[watchlist.Validation1.ValidContentId]]
[error]  required: akka.http.scaladsl.marshalling.ToResponseMarshallable
[error]                 complete(list)

Is there a way I can just marshall without asInstanceOf?

felher
@felher

Is there a catamorphism for coproducts? Something like:

val x: A :+: B :+: CNil
val i: Int = x.cata(a => 1, b => 2)

I would prefer that over using Poly because it's more readable (when having few cases) and also gives an compiler error if someone removes a case from the coproduct, which I think (although I have not tested it) using a poly wouldn't do.

Dominic Egger
@GrafBlutwurst
So I am playing around with a schema library and I currently think I need something like shapeless polyfunction. for that i need to build something like this https://scalafiddle.io/sf/UdAMWyZ/1 but I couldn't find out how to require a poly function to cover every case of a HList. Can anyone shed some light on this?
basically i use hlist to track the
Dominic Egger
@GrafBlutwurst
basically i wanna use hlist to accumulate all the possible cases
Dmytro Mitin
@DmytroMitin
@inanme Can't reproduce. Both options don't compile for me
Error:(32, 29) type mismatch;
 found   : List[String]
 required: akka.http.scaladsl.marshalling.ToResponseMarshallable
  complete(list.asInstanceOf[List[String]])
Error:(35, 12) type mismatch;
 found   : List[String with shapeless.tag.Tagged[App.ValidContentId]]
 required: akka.http.scaladsl.marshalling.ToResponseMarshallable
  complete(list)
Dmytro Mitin
@DmytroMitin
I assumed you have type ValidContentId or trait ValidContentId.
Dmytro Mitin
@DmytroMitin
@GrafBlutwurst You can check that there is implicit shapeless.ops.hlist.Mapper[P, L] where L is your hlist and P is your poly. Existence of such implicit means that P can be applied to all elements of L.
felher
@felher

Regarding my question about catamorphism: I have a workaround which doesn't allow handling of too many or too few cases and lets me write it as

      CFold.start[Int :+: String :+: CNil, Int]
        .add((i: Int) => i)
        .add((s: String) => s.length)
        .call(a)

where a is a value of type Int :+: String :+: CNil which is okay for now. But if there is something like a built-in catamorphism, I'd still be interested :)

Dmytro Mitin
@DmytroMitin
@felher What signature would "cata" have for arbitrary val x: C, C <: Coproduct? I guess the closest is shapeless.ops.coproduct.Mapper.
Dominic Egger
@GrafBlutwurst
@DmytroMitin oh thanks!
Dominic Egger
@GrafBlutwurst
looks like I have to prove that each case goes from some type to the same again and also in the node case that their left and right hlists are sublists
felher
@felher
@DmytroMitin That's a good question. I'm not sure how that would work. But I don't understand most of shapless macro magic, so my not understanding how that would work wasn't evidence that there isn't such a thing ;)
Georgi Krastev
@joroKr21
@felher There is a long-standing PR to add something like this which needs help: #632
felher
@felher
@joroKr21 Interesting. I'll take a look at that.