Skip to content

Commit 9397ee5

Browse files
jevonmaovarungandhi-applexwu
authored
[Diagnostics] Add educational notes explaining multiple inheritance (#38253)
* Add educational notes for multiple inheritance * Update edu note by adopting reviewer suggestions * Update userdocs/diagnostics/multiple-class-inheritance.md Co-authored-by: Varun Gandhi <[email protected]> * Added Flyable protocol definition to example * Apply suggestions from code review Co-authored-by: Varun Gandhi <[email protected]> * Apply suggestions from code review Co-authored-by: Xiaodi Wu <[email protected]> * Use consistent terminology Co-authored-by: Xiaodi Wu <[email protected]> * Use utensils example as suggested by review * Update title to Multiple Inheritance * Apply suggestions from code review Co-authored-by: Xiaodi Wu <[email protected]> * Rename file to match changed name * Add more objects in protocol construction example * Apply suggestions from code review Co-authored-by: Xiaodi Wu <[email protected]> Co-authored-by: Varun Gandhi <[email protected]> Co-authored-by: Xiaodi Wu <[email protected]>
1 parent bc55d02 commit 9397ee5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

include/swift/AST/EducationalNotes.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,8 @@ EDUCATIONAL_NOTES(result_builder_missing_build_either,
9191
"result-builder-methods.md")
9292
EDUCATIONAL_NOTES(result_builder_missing_build_array,
9393
"result-builder-methods.md")
94-
94+
95+
EDUCATIONAL_NOTES(multiple_inheritance,
96+
"multiple-inheritance.md")
97+
9598
#undef EDUCATIONAL_NOTES
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Multiple Inheritance
2+
3+
In some programming languages, a class can inherit the interface of multiple base classes. Known as multiple inheritance, this feature can add significant complexity to the language and is unsupported in Swift. Instead, Swift allows composition of interfaces using protocols.
4+
5+
Consider the following example:
6+
7+
```swift
8+
protocol Utensil {
9+
var name: String { get }
10+
}
11+
12+
protocol ServingUtensil: Utensil {
13+
func serve()
14+
}
15+
16+
extension ServingUtensil {
17+
func serve() { /* Default implementation. */ }
18+
}
19+
20+
protocol Fork: Utensil {
21+
func spear()
22+
}
23+
24+
protocol Spoon: Utensil {
25+
func scoop()
26+
}
27+
28+
struct CarvingFork: ServingUtensil, Fork { /* ... */ }
29+
struct Spork: Spoon, Fork { /* ... */ }
30+
struct Ladle: ServingUtensil, Spoon { /* ... */ }
31+
```
32+
33+
Swift protocols can declare interfaces that must be implemented by each conforming type (like abstract class members in other programming languages such as C# or Java), and they can also provide overridable default implementations for those requirements in protocol extensions.
34+
35+
When class inheritance and protocol conformances are used together, subclasses inherit protocol conformances from base classes, introducing additional complexity. For example, the default implementation of a protocol requirement not overridden in the conforming base class also cannot be overridden in any subclass ([SR-103](https://bugs.swift.org/browse/SR-103)).
36+
37+
To learn more about defining and adopting protocols, see the [Protocols](https://docs.swift.org/swift-book/LanguageGuide/Protocols.html) section in _The Swift Programming Language_. To learn more about class inheritance, see the [Inheritance](https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html) section in _The Swift Programming Language_.

0 commit comments

Comments
 (0)