Skip to content

Commit 9ef9efd

Browse files
Adjust wording for _disfavoredOverload to emphasize usage guidelines.
The historical context is primarily meant to guide usage, not as a fun aside.
1 parent 24d273f commit 9ef9efd

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

docs/ReferenceGuides/UnderscoredAttributes.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,21 @@ Marks an overload that the type checker should try to avoid using. When the
6666
expression type checker is considering overloads, it will prefer a solution
6767
with fewer `@_disfavoredOverload` declarations over one with more of them.
6868

69-
Historically, it was designed to help with `ExpressibleByXYZLiteral` types.
70-
The type checker strongly prefers to give literals their default type
71-
(e.g. `Int` for `ExpressibleByIntegerLiteral`,
72-
`String` for `ExpressibleByStringLiteral`, etc.).
73-
If an API should prefer some other type, but accept the default too,
74-
marking the declaration taking the default type with `@_disfavoredOverload`
75-
gives the desired behavior:
69+
Use `@_disfavoredOverload` to work around known bugs in the overload
70+
resolution rules that cannot be immediately fixed without a source break.
71+
Don't use it to adjust overload resolution rules that are otherwise sensible
72+
but happen to produce undesirable results for your particular API; it will
73+
likely be removed or made into a no-op eventually, and then you will be
74+
stuck with an overload set that cannot be made to function in the way
75+
you intend.
76+
77+
`@_disfavoredOverload` was first introduced to work around a bug in overload
78+
resolution with `ExpressibleByXYZLiteral` types. The type checker strongly
79+
prefers to give literals their default type (e.g. `Int` for
80+
`ExpressibleByIntegerLiteral`, `String` for `ExpressibleByStringLiteral`,
81+
etc.). If an API should prefer some other type, but accept the default too,
82+
marking the declaration taking the default type with `@_disfavoredOverload`
83+
gives the desired behavior:
7684

7785
```swift
7886
extension LocalizedStringKey: ExpressibleByStringLiteral { ... }
@@ -87,12 +95,6 @@ extension Text {
8795
}
8896
```
8997

90-
`@_disfavoredOverload` is a stop-gap design; eventually the type-checker
91-
behavior should be fixed and the attribute should be removed/made into a no-op.
92-
93-
Its main use case is to work around clearly undesirable overload resolution,
94-
not to arbitrarily manipulate overload priority.
95-
9698
## `@_dynamicReplacement(for:)`
9799

98100
Marks a function as the dynamic replacement for another `dynamic` function.

0 commit comments

Comments
 (0)