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.

Cyclic reference of extension method when exported and used at toplevel

See original GitHub issue

Compiler version

v3.1.0-RC2

Minimized code

https://scastie.scala-lang.org/upOXnTlLTgaIZoVUhlsMGg

trait MyExtensions:
  extension (lhs: Int) def bash: Unit = {}

object MyExtensions extends MyExtensions

object Internal:
  export MyExtensions.*
  val works = 1.bash

export MyExtensions.*
val fails = 1.bash

Output

-- [E046] Cyclic Error: test.scala:2:18 -------------------------------------------
2 |  extension (lhs: Int) def bash: Unit = {}
  |                  ^
  |                  Cyclic reference involving method bash

longer explanation available when compiling with `-explain`
-- [E008] Not Found Error: test.scala:11:14 ---------------------------------------
11 |val fails = 1.bash
   |            ^^^^^^
   |            value bash is not a member of Int

Expectation

No error. Note: if used in scastie in Worksheet mode, this example compiles successfully.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
soronpocommented, Oct 8, 2021

Thank you very much @smarter and @odersky !

0reactions
smartercommented, Oct 8, 2021

As a workaround, you can move the object definition ahead of the trait definition:

object MyExtensions extends MyExtensions
trait MyExtensions:
  extension (lhs: Int) def bash: Unit = {}

export MyExtensions.*
val fails = 1.bash

I’m also reopening since I have a tentative fix: https://github.com/lampepfl/dotty/pull/13719

Read more comments on GitHub >

github_iconTop Results From Across the Web

scala - Top level opaque type with same underlying type and ...
I ran into an issue when defining opaque types with identical underlying types and extension methods, e.g.: opaque type HandleA = Int object ......
Read more >
Extension method must be defined in a top level static class
Hi All,. I am trying to understanding extension method and it uses. So i wrote below code at C#.Net-4.7 sdk.
Read more >
Dart Static Extension Methods Design - language - GitHub
The extension methods are static, which means that we use the static type of an expression to figure out which method to call,...
Read more >
16. Modules - Exploring JS
Imports and exports must be at the top level; 16.3.4. Imports are hoisted; 16.3.5. Imports are read-only views on exports; 16.3.6. Support for...
Read more >
Extension Methods - Scala 3 - EPFL
The extension method is visible under a simple name, by being defined or inherited or imported in a scope enclosing the reference. The...
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