{"id":19370619,"url":"https://github.com/sophiecollard/variance","last_synced_at":"2025-10-10T13:06:25.767Z","repository":{"id":199339939,"uuid":"702671525","full_name":"sophiecollard/variance","owner":"sophiecollard","description":"Simple examples to illustrate the differences between invariance, covariance and contravariance in Scala","archived":false,"fork":false,"pushed_at":"2024-02-10T18:28:05.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-12T18:29:09.930Z","etag":null,"topics":["contravariance","covariance","fp","functional-programming","invariance","scala"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sophiecollard.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-10-09T19:09:45.000Z","updated_at":"2023-10-14T09:36:38.000Z","dependencies_parsed_at":"2023-10-15T09:43:33.191Z","dependency_job_id":"6080896c-c044-4861-add9-2bc2d3a98e09","html_url":"https://github.com/sophiecollard/variance","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"f22b514184360d8caa5eadd400b92fd3a7508954"},"previous_names":["sophiecollard/variance"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sophiecollard/variance","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sophiecollard%2Fvariance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sophiecollard%2Fvariance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sophiecollard%2Fvariance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sophiecollard%2Fvariance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sophiecollard","download_url":"https://codeload.github.com/sophiecollard/variance/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sophiecollard%2Fvariance/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004005,"owners_count":26083661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contravariance","covariance","fp","functional-programming","invariance","scala"],"created_at":"2024-11-10T08:16:06.830Z","updated_at":"2025-10-10T13:06:25.762Z","avatar_url":"https://github.com/sophiecollard.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Variance\n\nSimple examples to illustrate the differences between invariance, covariance and contravariance in Scala.\n\n## Example 1\n\nConsider the following `Animal` type and `Rescue` and `Clinic` type classes:\n\n```scala\nsealed abstract class Animal(val name: String)\n\nobject Animal:\n\n  final case class Cat(\n    override val name: String,\n    livesRemaining: Int\n  ) extends Animal(name)\n\n  final case class Dog(\n    override val name: String,\n    breed: Option[DogBreed]\n  ) extends Animal(name)\n```\n\n```scala\ntrait Rescue[+A]:\n  extension (name: String) def adopt: A\n\ntrait Clinic[-A]:\n  extension (a: A) def examine: String\n```\n\nNow, let's define methods to adopt an animal and take a dog to the vet:\n\n```scala\ndef adopt(name: String)(using rescue: Rescue[Animal]): Animal =\n  rescue.adopt(name)\n\ndef takeToTheVet(dog: Dog)(using clinic: Clinic[Dog]): String =\n  clinic.examine(dog)\n```\n\nAssuming instances of `Rescue[Animal]` and `Clinic[Dog]` have been defined, we could invoke the above methods as\nfollows:\n\n```scala\nval teddy = adopt(\"Teddy\")\nprintln(s\"Welcome home ${teddy.name}!\")\n\nval médor = Dog(name = \"Médor\", breed = Some(DogBreed.Labrador))\ntakeToTheVet(médor)\n```\n\nLet's assume however, that we do not have `Rescue[Animal]` nor `Clinic[Dog]` instances. Instead, all we have are\n`Rescue[Dog]` and `Clinic[Animal]`:\n\n```scala\nval teddy = adopt(\"Teddy\")(using summon[Rescue[Dog]])\nprintln(s\"Welcome home ${teddy.name}!\")\n\nval médor = Dog(name = \"Médor\", breed = Some(DogBreed.Labrador))\ntakeToTheVet(médor)(using summon[Clinic[Animal]])\n```\n\nThe code above is able to compile thanks to `Rescue` being _covariant_ in `A` and `Clinic` being _contravariant_ in `A`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsophiecollard%2Fvariance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsophiecollard%2Fvariance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsophiecollard%2Fvariance/lists"}