optics-dev / optics-dev/Monocle

Compilation error with context bounds

Open
#1,259 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Scala
Stars
1.7k
Forks
207
Avg merge
7h 59m
Merged PRs (30d)
5

Description

Hello there!

Compilation of the following code will fail

import monocle.Lens
import monocle.macros.GenLens

trait Foo[T]
trait Bar[T]

case class A[T: Foo](s: A.S[T]) {
  private val lens: Lens[A.S[T], Bar[T]] = GenLens[A.S[T]](_.bar)
}

object A {
  case class S[T: Foo](bar: Bar[T])
}

in Scala 3 with the following error

[error] 8 |  private val lens: Lens[A.S[T], Bar[T]] = GenLens[A.S[T]](_.bar)
[error]   |                                           ^^^^^^^^^^^^^^^^^^^^^^
[error]   |Exception occurred while executing macro expansion.
[error]   |java.lang.Exception: Expected an expression. This is a partially applied Term. Try eta-expanding the term first.

and in Scala 2.13 with

[error] could not find implicit value for evidence parameter of type Foo[T]
[error]   private val lens: Lens[A.S[T], Bar[T]] = GenLens[A.S[T]](_.bar)
[error]                                                           ^

Interestingly, the last version that appears to work is 1.5.1-cats on Scala 2.11 (but not on Scala 2.12.)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the minimal example around GenLensA.S[T] on the reported Scala versions, comparing the Scala 2.11, 2.12, 2.13, and Scala 3 results. Inspect how the macro handles the context bounds on A and A.S. Done means the example compiles on the supported affected versions and has a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.