Skip to content

Commit d4fca54

Browse files
committed
Add clause to docs
1 parent 6310ec2 commit d4fca54

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

docs/docs/reference/other-new-features/targetName.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,31 @@ def f(x: => Int): Int = x + 1 // OK
5656
```
5757
This will produce methods `f_string` and `f` in the generated code.
5858

59+
However, `@targetName` annotations are not allowed to break overriding relationships
60+
between two definitions that have otherwise the same names and types. So the following would be in error:
61+
```scala
62+
import annotation.targetName
63+
class A:
64+
def f(): Int = 1
65+
class B extends A:
66+
targetName("g") def f(): Int = 2
67+
```
68+
The compiler reports here:
69+
```
70+
-- Error: test.scala:6:23 ------------------------------------------------------
71+
6 | @targetName("g") def f(): Int = 2
72+
| ^
73+
|error overriding method f in class A of type (): Int;
74+
| method f of type (): Int should not have a @targetName annotation since the overridden member hasn't one either
75+
```
76+
The relevant overriding rules can be summarized as follows:
77+
78+
- Two members can override each other if their names and signatures are the same,
79+
and they either have the same erased names or the same types.
80+
- If two members override, then both their erased names and their types must be the same.
81+
5982
As usual, any overriding relationship in the generated code must also
60-
be present in the original code. So the following example would be in error:
83+
be present in the original code. So the following example would also be in error:
6184
```scala
6285
import annotation.targetName
6386
class A:

0 commit comments

Comments
 (0)