|
| 1 | +-- [E120] Naming Error: tests/neg/i23832a.scala:9:8 -------------------------------------------------------------------- |
| 2 | +9 | given Special[Option[Int]] = ??? // error |
| 3 | + | ^ |
| 4 | + | Conflicting definitions: |
| 5 | + | final lazy given val given_Special_Option: Special[Option[Long]] in object syntax at line 8 and |
| 6 | + | final lazy given val given_Special_Option: Special[Option[Int]] in object syntax at line 9 |
| 7 | + |--------------------------------------------------------------------------------------------------------------------- |
| 8 | + | Explanation (enabled by `-explain`) |
| 9 | + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 10 | + | |
| 11 | + | As part of the Scala compilation pipeline every type is reduced to its erased |
| 12 | + | (runtime) form. In this phase, among other transformations, generic parameters |
| 13 | + | disappear and separate parameter-list boundaries are flattened. |
| 14 | + | |
| 15 | + | For example, both `f[T](x: T)(y: String): Unit` and `f(x: Any, z: String): Unit` |
| 16 | + | erase to the same runtime signature `f(x: Object, y: String): Unit`. Note that |
| 17 | + | parameter names are irrelevant. |
| 18 | + | |
| 19 | + | In your code the two declarations |
| 20 | + | |
| 21 | + | final lazy given val given_Special_Option: Special[Option[Long]] |
| 22 | + | final lazy given val given_Special_Option: Special[Option[Int]] |
| 23 | + | |
| 24 | + | erase to the identical signature |
| 25 | + | |
| 26 | + | Special |
| 27 | + | |
| 28 | + | so the compiler cannot keep both: the generated bytecode symbols would collide. |
| 29 | + | |
| 30 | + | To fix this error, you need to disambiguate the two definitions. You can either: |
| 31 | + | |
| 32 | + | 1. Rename one of the definitions, or |
| 33 | + | 2. Keep the same names in source but give one definition a distinct |
| 34 | + | bytecode-level name via `@targetName`; for example: |
| 35 | + | |
| 36 | + | @targetName("given_Special_Option_2") |
| 37 | + | final lazy given val given_Special_Option: Special[Option[Int]] |
| 38 | + | |
| 39 | + | 3. Provide an explicit, unique name to given definitions, since the names |
| 40 | + | assigned to anonymous givens may clash. For example: |
| 41 | + | |
| 42 | + | given myGiven: Special[Option[Int]] |
| 43 | + | |
| 44 | + | Choose the `@targetName` argument carefully: it is the name that will be used |
| 45 | + | when calling the method externally, so it should be unique and descriptive. |
| 46 | + --------------------------------------------------------------------------------------------------------------------- |
0 commit comments