Skip to content

Commit 083dbd5

Browse files
committed
Fixes to named annotations example
1 parent 67fd0de commit 083dbd5

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

_posts/2024-12-05-release-notes-3.6.2.md

+30-14
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,45 @@ Let's take the following example:
186186
```
187187

188188
Reordering the fields is binary-compatible but it might affect the meaning of `@Annotation(1)`
189-
Starting from Scala 3.6, named arguments are required for Java-defined annotations that define multiple parameters. Java defined annotations with a single parameter named `value` can still be used anonymously.
189+
Starting from Scala 3.6, named arguments are required for Java-defined annotations that define multiple parameters.
190+
If the Java-defined annotation contains paramter named `value` its name can be ommited only when annotation is applied using a single argument.
190191

191192
```Java
192-
public @interface CanonicalAnnotation {
193+
public @interface Example {
193194
String value() default "";
195+
String param() default "";
194196
}
195-
public @interface CustomAnnotation {
197+
public @interface NoValueExample {
196198
String param() default "";
197199
}
198200
```
199201

200202
```Scala
201-
class NoExplicitNames(
202-
@CanonicalAnnotation() useDefault: String,
203-
@CanonicalAnnotation(value = "myValue") named: String
204-
@CanonicalAnnotation("myValue") unnamed: String
205-
)
206-
207-
class ExplicitNamesRequired(
208-
@CustomAnnotation() useDefault: String,
209-
@CustomAnnotation(param = "myParam") named: String
210-
@CustomAnnotation("unnamedParam") invalid: String // error
211-
)
203+
// Annotation with `value: String = "", param: String = ""` paramters
204+
@Example()
205+
def onlyDefaults: Unit = ()
206+
207+
@Example("param")
208+
def valueWithDefaults: Unit = ()
209+
210+
@Example(value = "ok", param = "fine")
211+
def multipleParams: Unit = ()
212+
213+
@Example("a", "b") // error, both parameters should be named
214+
def multipleUnnamedParams: Unit = ()
215+
216+
@Example("first", param = "second") // error, `"first"` argument should be named
217+
def multipleMixedParams: Unit = ()
218+
219+
// Annotation with `param: String = ""` parameters
220+
@NoValueExample()
221+
def defaultOnly: Unit = ()
222+
223+
@NoValueExample(param = "foo")
224+
def namedParam: Unit = ()
225+
226+
@NoValueExample("foo") // error, the only parameter is not named `value`
227+
def invalidUnnamedParam: Unit = ()
212228
```
213229

214230
The compiler can provide you with automatic rewrites introducing now required names, using `-source:3.6-migration, -rewrite` flags. The rewrites are done on a best-effort basis and should be inspected for correctness by the users.

0 commit comments

Comments
 (0)