question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scala 2 regression: Private implicit conversion in companion object scope is not found inside companion class

See original GitHub issue

Compiler version

3.1.0 and 3.1.1-RC1

Minimized code

import LightTypeTagInheritance._
// import LightTypeTagInheritance.Ctx // this doesn't work either

trait LightTypeTagRef

object LightTypeTagInheritance {
  private final case class Ctx(self: LightTypeTagInheritance) {
    def next(): Ctx = Ctx(self)
  }
  private implicit final class CtxExt(private val ctx: Ctx) extends AnyVal {
    def isChild(selfT0: LightTypeTagRef, thatT0: LightTypeTagRef): Boolean = ctx.self.isChild(ctx.next())(selfT0, thatT0)
  }
}

class LightTypeTagInheritance {
  
  def isChild(s: LightTypeTagRef, t: LightTypeTagRef): Boolean = {
    isChild(new Ctx(this))(s, t)
  }
  
  private def isChild(ctx: Ctx)(s: LightTypeTagRef, t: LightTypeTagRef): Boolean = {
    ctx.isChild(s, t)
  }
  
}

object App extends App {
  println(LightTypeTagInheritance)
}

Scastie: https://scastie.scala-lang.org/BfgPFl4wR9KewXSVPFCXFg

Output

value isChild is not a member of LightTypeTagInheritance.Ctx, but could be made available as an extension method.

The following import might fix the problem:

  import LightTypeTagInheritance.CtxExt

Expectation

Expected to compile, as in Scala 2 (https://scastie.scala-lang.org/urqciNvJRPWdhYPH8SGrjQ)

Workaround: changing CtxExt from private to private[LightTypeTagInheritance] fixes the issue

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
prolativcommented, Nov 30, 2021

Minimized example:

object Foo {
  case class Bar(i: Int)

  private implicit class BarOps(bar: Bar) {
    def twice = Bar(bar.i * 2)
  }
}

class Foo {
  def bar = Foo.Bar(1).twice
}

object App extends App {
  println((new Foo).bar)
}
1reaction
prolativcommented, Nov 29, 2021

Interestingly this works if you move import LightTypeTagInheritance._ from the top level to the inside of class LightTypeTagInheritance. Honestly speaking I’m not sure if this is a bug or a feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implicit conversion declared in companion object is not called
So, compiler tries to find implicit conversion from "Fraction to Int" in "Int" type first. As "Int" type does not have any implementation...
Read more >
implicit object on a companion is not found in implicit scope
I suspect this could be one of two things: object is treated separately to val; the object is a type in its own...
Read more >
Chapter 5. Using implicits to write expressive code - Scala in ...
The Scala compiler can infer one of two situations: A method call or constructor with a missing parameter. Missing conversion from one type...
Read more >
Ammonite
Ammonite provides a set of magic imports that let you load additional code into a REPL session: these are imports which start with...
Read more >
Kotlin Language Documentation 1.7.21
Extension properties. Companion object extensions. Scope of extensions. Declaring extensions as members. Note on visibility. Data classes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found