Skip to content

Support covariant typeclass families #627

@nigredo-tori

Description

@nigredo-tori

This issue is motivated by the Caliban library, which defines a Schema typeclass like this:

abstract class Schema[-R, T]

R can be different for different T, but we can combine heterogenous instances. For example, for some case class with fields that have corresponding instances Schema[R1, Field1] and Schema[R2, Field2] we can build a Schema[R1 & R2, SomeCaseClass]. However, as it stands right now, we can't easily derive it using Magnolia, because we have Schema.Typeclass defined as a Schema[Any, *], so that is what gen produces.

The way Caliban "solves" this is by providing a GenericSchema helper to the library user, with its own specialized Typeclass. It gets awkward to use without giving up the way ZIO code tends to be structured. I won't get into details, suffice to say that I'd like to only have to rely on Schema.gen for derivation.

I imagine that everywhere where we use Typeclass (e.g. here that is what typeConstructor is bound to), we can instead use a narrower type. It's a bit awkward to explain, so I'll add a PR for Scala 2 shortly.

Does this change fit this library, or is it specifically geared towards invariant typeclasses? I feel like with the rise of ZIO we are likely to see more of covariance everywhere, so cases like this could get somewhat more common.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions