You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, the `++=` operation is implemented (in Byte code or native code) under the name `append`. The implementation name affects the code that is generated, and is the name under which code from other languages can call the method. For instance, `++=` could be invoked from Java like this:
15
-
```
17
+
18
+
```java
16
19
VecOps.append(vec1, vec2)
17
20
```
21
+
18
22
The `@targetName` annotation has no bearing on Scala usages. Any application of that method in Scala has to use `++=`, not `append`.
19
23
20
24
### Details
@@ -38,58 +42,69 @@ The `@targetName` annotation has no bearing on Scala usages. Any application of
38
42
39
43
`@targetName` annotations are significant for matching two method definitions to decide whether they conflict or override each other. Two method definitions match if they have the same name, signature, and erased name. Here,
40
44
41
-
- The _signature_ of a definition consists of the names of the erased types of all (value-) parameters and the method's result type.
42
-
- The _erased name_ of a method definition is its target name if a `@targetName`
43
-
annotation is given and its defined name otherwise.
45
+
- The _signature_ of a definition consists of the names of the erased types of all (value-) parameters and the method's result type.
46
+
- The _erased name_ of a method definition is its target name if a `@targetName`
47
+
annotation is given and its defined name otherwise.
44
48
45
49
This means that `@targetName` annotations can be used to disambiguate two method definitions that would otherwise clash. For instance.
50
+
46
51
```scala
47
52
deff(x: =>String):Int= x.length
48
53
deff(x: =>Int):Int= x +1// error: double definition
49
54
```
55
+
50
56
The two definitions above clash since their erased parameter types are both `Function0`, which is the type of the translation of a by-name-parameter. Hence
51
57
they have the same names and signatures. But we can avoid the clash by adding a `@targetName` annotation to either method or to both of them. E.g.
58
+
52
59
```scala
53
60
@targetName("f_string")
54
61
deff(x: =>String):Int= x.length
55
62
deff(x: =>Int):Int= x +1// OK
56
63
```
64
+
57
65
This will produce methods `f_string` and `f` in the generated code.
58
66
59
67
However, `@targetName` annotations are not allowed to break overriding relationships
60
68
between two definitions that have otherwise the same names and types. So the following would be in error:
0 commit comments