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:
20
-
```
21
-
VecOps.append(vec1, vec2)
22
-
```
23
-
The `@alpha` annotation has no bearing on Scala usages. Any application of that method in Scala has to use `++=`, not `append`.
24
-
25
-
### Motivation
26
-
27
-
The `@alpha` annotation serves a dual purpose:
28
-
29
-
- It helps interoperability between Scala and other languages.
30
-
- It serves as a documentation tool by providing an alternative regular name
31
-
as an alias of a symbolic operator.
32
-
33
-
### Details
34
-
35
-
1.`@alpha` is defined in package `scala.annotation`. It takes a single argument
36
-
of type `String`. That string is called the _external name_ of the definition
37
-
that's annotated.
38
-
39
-
2. An `@alpha` annotation can be given for all kinds of definitions.
40
-
41
-
3. The name given in an `@alpha` annotation must be a legal name
42
-
for the defined entities on the host platform.
43
-
44
-
4. Definitions with symbolic names should have an `@alpha` annotation. Lack of such
45
-
an annotation will raise a deprecation warning.
46
-
47
-
5. Definitions with names in backticks that are not legal host platform names
48
-
should have an `@alpha` annotation. Lack of such an annotation will raise a deprecation warning.
49
-
50
-
6.`@alpha` annotations must agree: If two definitions are members of an object or class with the same name and matching types, then either none of them has an `@alpha` annotation, or both have `@alpha` annotations with the same name.
51
-
52
-
7. There must be a one-to-one relationship between external and internal names:
53
-
If two definitions are members of an object or class with matching types and both have `@alpha` annotations with the same external name, then their internal method names must also be the same.
8
+
First, an alphanumeric method can be used as an infix operator only if its definition carries an `@infix` annotation. Second, it is recommended (but not enforced) to
9
+
augment definitions of symbolic operators with `@targetName` annotations. Finally,
10
+
a syntax change allows infix operators to be written on the left in a multi-line expression.
54
11
55
12
## The @infix Annotation
56
13
57
14
An `@infix` annotation on a method definition allows using the method as an infix operation. Example:
58
15
```scala
59
-
importscala.annotation.alpha
16
+
importscala.annotation.{infix, targetName}
60
17
61
18
traitMultiSet[T] {
62
19
@@ -65,7 +22,7 @@ trait MultiSet[T] {
65
22
66
23
defdifference(other: MultiSet[T]):MultiSet[T]
67
24
68
-
@alpha("intersection")
25
+
@targetName("intersection")
69
26
def*(other: MultiSet[T]):MultiSet[T]
70
27
}
71
28
@@ -133,6 +90,19 @@ The purpose of the `@infix` annotation is to achieve consistency across a code b
133
90
5. To smooth migration to Scala3.0, alphanumeric operators will only be deprecated from Scala3.1 onwards,
134
91
or if the `-source 3.1` option is giveninDotty/Scala3.
135
92
93
+
##The@targetName Annotation
94
+
95
+
It is recommended that definitions of symbolic operators carry a [@targetName annotation](../other-new-features/targetName.html) that provides an encoding of the operator with an alphanumeric name. This has several benefits:
96
+
97
+
-It helps interoperability between Scala and other languages. One can call
98
+
a Scala-defined symbolic operator from another language using its target name,
99
+
which avoids having to remember the low-level encoding of the symbolic name.
100
+
-It helps legibility of stacktraces and other runtime diagnostics, where the
101
+
user-defined alphanumeric name will be shown instead of the low-level encoding.
102
+
-It serves asa documentation tool by providing an alternative regular name
103
+
asan alias of a symbolic operator. This makes the definition also easier
104
+
to find in a search.
105
+
136
106
##SyntaxChange
137
107
138
108
Infix operators can now appear at the start of lines in a multi-line expression. Examples:
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
+
```
16
+
VecOps.append(vec1, vec2)
17
+
```
18
+
The `@targetName` annotation has no bearing on Scala usages. Any application of that method in Scala has to use `++=`, not `append`.
19
+
20
+
### Details
21
+
22
+
1.`@targetName` is defined in package `scala.annotation`. It takes a single argument
23
+
of type `String`. That string is called the _external name_ of the definition
24
+
that's annotated.
25
+
26
+
2. A `@targetName` annotation can be given for all kinds of definitions.
27
+
28
+
3. The name given in a `@targetName` annotation must be a legal name
29
+
for the defined entities on the host platform.
30
+
31
+
4. It is recommended that definitions with symbolic names have a `@targetName` annotation. This will establish an alternate name that is easier to search for and
32
+
will avoid cryptic encodings in runtime diagnostics.
33
+
34
+
5. Definitions with names in backticks that are not legal host platform names
35
+
should also have a `@targetName` annotation.
36
+
37
+
### Relationship with Overriding
38
+
39
+
`@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
+
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.
44
+
45
+
This means that `@targetName` annotations can be used to disambiguate two method definitions that would otherwise clash. For instance.
46
+
```scala
47
+
deff(x: =>String):Int= x.length
48
+
deff(x: =>Int):Int= x +1// error: double definition
49
+
```
50
+
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
+
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.
52
+
```scala
53
+
@targetName("f_string")
54
+
deff(x: =>String):Int= x.length
55
+
deff(x: =>Int):Int= x +1// OK
56
+
```
57
+
This will produce methods `f_string` and `f` in the generated code.
58
+
59
+
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:
61
+
```scala
62
+
importannotation.targetName
63
+
classA:
64
+
deff():Int=1
65
+
classBextendsA:
66
+
targetName("f") defg():Int=2
67
+
```
68
+
Here, the original methods `g` and `f` do not override each other since they have
69
+
different names. But once we switch to target names, there is a clash that is reported by the compiler:
Copy file name to clipboardExpand all lines: docs/docs/reference/overview.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,8 +58,8 @@ These constructs are restricted to make the language safer.
58
58
-[Given Imports](contextual/given-imports.md): implicits now require a special form of import, to make the import clearly visible.
59
59
-[Type Projection](dropped-features/type-projection.md): only classes can be used as prefix `C` of a type projection `C#A`. Type projection on abstract types is no longer supported since it is unsound.
60
60
-[Multiversal Equality](contextual/multiversal-equality.md) implements an "opt-in" scheme to rule out nonsensical comparisons with `==` and `!=`.
make method application syntax uniform across code bases.
63
63
64
64
Unrestricted implicit conversions continue to be available in Scala 3.0, but will be deprecated and removed later. Unrestricted versions of the other constructs in the list above are available only under `-source 3.0-migration`.
65
65
@@ -109,6 +109,7 @@ These are additions to the language that make it more powerful or pleasant to us
109
109
-[Dependent Function Types](new-types/dependent-function-types.md) generalize dependent methods to dependent function values and types.
110
110
-[Polymorphic Function Types](https://github.com/lampepfl/dotty/pull/4672) generalize polymorphic methods to dependent function values and types. _Current status_: There is a proposal, and a prototype implementation, but the implementation has not been finalized or merged yet.
111
111
-[Kind Polymorphism](other-new-features/kind-polymorphism.md) allows the definition of operators working equally on types and type constructors.
112
+
-[@targetName Annotations](other-new-features/targetName.md) make it easier to interoperate with code written in other languages and give more flexibility for avoiding name clashes.
0 commit comments