Skip to content

Support covariant typeclass families in Scala 2#628

Merged
adamw merged 5 commits intosoftwaremill:scala2from
nigredo-tori:covariant-typeclass
Feb 26, 2026
Merged

Support covariant typeclass families in Scala 2#628
adamw merged 5 commits intosoftwaremill:scala2from
nigredo-tori:covariant-typeclass

Conversation

@nigredo-tori
Copy link
Copy Markdown

See #627

object Magnolia {
import CompileTimeState._

private[Magnolia] type ConstNothing[a] = Nothing
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A "typeclass" that is a subtype of any typeclass.

val subtypeObjects = typeclasses.zipWithIndex.map { case ((subType, typeclass), idx) =>
val symbol = subType.typeSymbol
val (annotationTrees, inheritedAnnotationTrees) = annotationsOf(symbol)
q"""$arrayVal($startVal + $idx) = $SubtypeObj[$typeConstructor, $genericType, $subType](
Copy link
Copy Markdown
Author

@nigredo-tori nigredo-tori Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "hard" type specifications where possible.


val paramsValDef = {
val method = TermName(if (isReadOnly) "readOnlyParams" else "params")
q"$PartsObj.$method[$typeConstructor, $genericType](..$paramsItems)"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When building paramsVal, we simultaneously let the typer calculate the narrowest subtype of the original Typeclass we can fit over the params. We can then use it instead of the original Typeclass.

/** Helpers to guide `Param`/`Subtype` array types and to provide access to the resulting typeclass.
*/
object Parts {
def params[TcLower[_], T]: PartiallyApplied[Param[*[_], T], TcLower] = new PartiallyApplied[Param[*[_], T], TcLower]()
Copy link
Copy Markdown
Author

@nigredo-tori nigredo-tori Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the magic that lets the typer choose a narrower typeclass, while still being constrained by the original Typeclass (TcLower here).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are great comments - can you maybe put them as comments on code? they help to understand what's going on :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I've also done some cleanup.

@adamw
Copy link
Copy Markdown
Member

adamw commented Feb 23, 2026

It seems the build fails?

@nigredo-tori
Copy link
Copy Markdown
Author

nigredo-tori commented Feb 24, 2026

It seems the build fails?

Not because of anything in the PR:

The job has exceeded the maximum execution time while awaiting a runner for 24h0m0s

It seems that the check uses the runners assigned to your repository, and I can't restart it (which makes sense). I see that you've already tried to restart it with the same result. My best guess is that this is caused by ubuntu-20.04 images being discontinued for GitHub Actions. You've changed the images for the scala3 branch but not for scala2.

nigredo-tori added a commit to nigredo-tori/magnolia that referenced this pull request Feb 24, 2026
adamw added a commit that referenced this pull request Feb 24, 2026
## Summary
- Replace inline `ci` job with reusable `build-scala.yml` workflow
(matches scala3 branch)
- Update `mima` and `publish` runners from discontinued `ubuntu-20.04`
to `ubuntu-22.04`
- Add `label` and `auto-merge` jobs for Scala Steward PRs (matches
scala3 branch)

Fixes the "maximum execution time while awaiting a runner" issue
reported in #628.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@adamw
Copy link
Copy Markdown
Member

adamw commented Feb 24, 2026

Ah of course, thanks for pointing that out. I think the build should be updated now

@adamw adamw merged commit 545a975 into softwaremill:scala2 Feb 26, 2026
5 checks passed
@adamw
Copy link
Copy Markdown
Member

adamw commented Feb 26, 2026

Thanks! :)

@nigredo-tori nigredo-tori deleted the covariant-typeclass branch February 26, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants