Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
Luka Jacobowitz
T is a subtype of burger, so a burger is not necessarily a T
You should make T a Supertype for that to work
Harmeet Singh(Taara)
got it, Covariance position define super type not sub-type, am I right ?
@harmeetsingh0013 The + for covariance and the <: for sub type are independent, you can have one or the other, and they mean separate things
class Store[+T] means "a Store[A] is a Store[B] when A is a B", this is covariance.
This is often associated with "getting" or "reading" information - if Store[A] is a "source" of something, you know it always produces instances of A. So if A <: B, all A's are also B's. So it's sensible to say that a Store[A] can safely be treated as a Store[B] - a Store[A] will always produce A's, and so will always produce B's as well, making it a Store[B]
Contravariance is the other way around, say you have a class that accepts instances of A - in that case if it can accept any A, and B <: A, it can accept any B as well (because every B is an A). So we have Target[A] is a Target[B] if B <: A.
Now you have Store[T <: Burger] - that says nothing about the variance (co or contra) of Store at all - it just says that you can only define a Store[T] if T <: Burger, i.e. for some type T that is a Burger.
Your original definition class Store[+T <: Burger] means "A store has a type parameter T, which must be some kind of Burger, and a Store[A] can be treated as a Store[B] as long as A is a B". So for example a Store[SuperFancyBurger] is still a Store[Burger], but we do need to know that SuperFancyBurger <: Burger. However you can't make a Store[Pizza] because Pizza is not a type of Burger.
Wow those explanations were rambling and terrible, I apologise. Hopefully some of that makes sense.
I'm trying to create Show instance for my custom config
I'm trying to use import cats.derived.semi from "org.typelevel" %% "kittens" % "1.2.0"
The complete code is
    import enumeratum.{Enum, EnumEntry}

    sealed abstract class AppEnvironment extends EnumEntry

    object AppEnvironment extends Enum[AppEnvironment] {
    case object Local extends AppEnvironment
    case object Testing extends AppEnvironment
    case object Production extends AppEnvironment

    override val values: Vector[AppEnvironment] =

    import java.net.InetAddress
    import scala.concurrent.duration.Duration

    final case class ApiConfig(host: InetAddress, port: Int, apiKey: String, timeout: Duration)

    import java.net.InetAddress
    import cats.Show
    import cats.derived.semi
    import ciris.config.loader.AppEnvironment.{Local, Production, Testing}
    import enumeratum.EnumEntry
    import eu.timepit.refined.auto._
    import eu.timepit.refined.types.string.NonEmptyString
    import scala.concurrent.duration._

    final case class Config(appName: NonEmptyString, environment: AppEnvironment, api: ApiConfig)

    object Config {
    implicit val showConfig: Show[Config] = {
        implicit val showDuration: Show[Duration] =

        implicit val showInetAddress: Show[InetAddress] =

        implicit def showEnumEntry[E <: EnumEntry]: Show[E] =

        // Show.show[Config](x => s"api = ${x.api} appName = ${x.appName} environment ${x.environment}")
The compiler error I'm getting is -
        [error] /Users/rajkumar.natarajan/Documents/Coding/kafka_demo/circe-demo/src/main/scala/ciris/config/loader/Config.scala:32:5: ambiguous implicit values:
        [error]  both value emptyProductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.HNil]
        [error]  and method emptyCoproductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.CNil]
        [error]  match expected type cats.derived.MkShow[A]
        [error]     show
        [error]     ^
        [error] one error found
        [error] (Compile / compileIncremental) Compilation failed
Any idea how can I resolve this error ?
Try deleting the implicit for showDuration. I believe an instance was added with this github issue (typelevel/cats#2216), so you might have two instances. I was able to compile the code, so let me know if not.
Forgot to mention to add import cats.implicits._
Abhishek Srivastava
Hello, How do I convert a Map[String, ConnectionIO[List[Foo]]] into ConnectionIO[Map[String, List[Foo]]]. Here connection IO is a monad used by doobie.
Rob Norris
Probably the easiest way is to add alleycats-core and import alleycats.std.map._ and then you can say .sequence
It's not strictly lawful because map traversal isn't congruent with equality and you can observe this with the Traverse instance. But it usually doesn't matter.
Abhishek Srivastava
great @tpolecat let me try this out. didn't know about alleycats :)
Minh Thai
hi, anyone know where can I buy a physical copy of the book Scala with Cats?
Rob Norris
@noelwelsh and/or @davegurnell ^^
Adam Rosien
@mt40 we occasionally print copies of the books, but we don't currently have a way to publish on demand. sorry!
the book is free, so if you really wanted to, you could get it printed
Ryan Zeigler
So, lets say I have a tagless encoding and I would like to add a localized state effect. The easy way is just to stack a StateT on top of my F, but if I wanted to use something like meow-mtl, does anyone know might I go about doing it? The only thing I can think of introducing some additional typeclass that knows how to convert F ~> G to add the state effect with a function receives a by name G[A] and an initial S returning an F[A]. This seems straightforward, but there doesn't seem to be a great way of introducing multiple local effects and seems generally kind of awkward. Does anyone have any pointers on a way of doing it?
Fabio Labella
A couple of thoughts:
  • there are cases in which lack of higher-rank types and implicit function types really hurts, and you are forced to stick with concrete types
  • I don't see how F ~> G comes into place in this specific scenario
My gut feeling is that you're in case no 1 and need both higher ranks and implicit function types, in which case I'd recommend just using the transformer locally instead (so StateT in this case). Anyway a small example would help
Ryan Zeigler
So, I don't actually need the performance, I was just trying to figure out how to do it
The F, and G came into play, because there could be generally F and G = StateT[F, S, ?] which gives me G: MonadState, alternatively, the meow-mtl ref constructs all F = IO and G = IO and there's just an implicit MonadState floating around that works for "G"
Ryan Zeigler
maybe the solution is just have a specific wrapper that knows how to bootstrap a state context for a set of operations and then delegates to some abstract implementation that requires the effect types. Then, the bootstrapper can be specialized for the base F type and the logical piece remains generic.
Jakub Kozłowski
if you're using meow-mtl, you don't need a G
I mean... you can have a MonadState for your F that is IO
created from a Ref[IO]
alternatively you can have MonadState for F and F = StateT[IO
Ryan Zeigler
yes, what I was trying to figure out how to do was introduce MonadState for any F locally while using meow-mtl when F = IO but keeping the encapsulating code unaware over the distinction
so, in that scenario, F might be G if you are using IO, but otherwise it won't
Jakub Kozłowski
ok, I see what you're trying to do
btw. does this work?
      def localState[A](initial: S)(runState: MonadState[IO, S] => IO[A]): IO[A] =
ah runState is from meow
Fabio Labella

so, I don't actually need the performance, I was just trying to figure out how to do it

I'm not talking about perf at all, just expressiveness