@@ -1237,21 +1237,20 @@ There are three virtual modifier keywords:
1237
1237
virtual" but is called
1238
1238
[ "pure virtual" in C++] ( https://en.wikipedia.org/wiki/Virtual_function#Abstract_classes_and_pure_virtual_functions ) .
1239
1239
Only abstract classes may have unimplemented abstract methods.
1240
- - ` impl ` - This marks a method that overrides a method marked ` virtual ` or
1240
+ - ` override ` - This marks a method that overrides a method marked ` virtual ` or
1241
1241
` abstract ` in the base class with an implementation specific to -- and
1242
1242
defined within -- this class. The method is still virtual and may be
1243
1243
overridden again in subsequent derived classes if this is a base class. See
1244
1244
[ method overriding in Wikipedia] ( https://en.wikipedia.org/wiki/Method_overriding ) .
1245
1245
Requiring a keyword when overriding allows the compiler to diagnose when the
1246
1246
derived class accidentally uses the wrong signature or spelling and so
1247
- doesn't match the base class. We intentionally use the same keyword here as
1248
- for implementing interfaces, to emphasize that they are similar operations.
1247
+ doesn't match the base class.
1249
1248
1250
- | Keyword on<br />method in ` C ` | Allowed in<br />` abstract class C ` | Allowed in<br />` base class C ` | Allowed in<br />final ` class C ` | in ` B ` where<br />` C ` extends ` B ` | in ` D ` where<br />` D ` extends ` C ` |
1251
- | ----------------------------- | ---------------------------------- | ------------------------------ | ------------------------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------- |
1252
- | ` virtual ` | ✅ | ✅ | ❌ | _ not present_ | ` abstract ` <br />` impl ` <br />_ not mentioned_ |
1253
- | ` abstract ` | ✅ | ❌ | ❌ | _ not present_ <br />` virtual ` <br />` abstract ` <br />` impl ` | ` abstract ` <br />` impl ` <br />_ may not be<br />mentioned if<br />` D ` is not final_ |
1254
- | ` impl ` | ✅ | ✅ | ✅ | ` virtual ` <br />` abstract ` <br />` impl ` | ` abstract ` <br />` impl ` |
1249
+ | Keyword on<br />method in ` C ` | Allowed in<br />` abstract class C ` | Allowed in<br />` base class C ` | Allowed in<br />final ` class C ` | in ` B ` where<br />` C ` extends ` B ` | in ` D ` where<br />` D ` extends ` C ` |
1250
+ | ----------------------------- | ---------------------------------- | ------------------------------ | ------------------------------- | ------------------------------------------------------------ | ---- -------------------------------------------------------------------------------- |
1251
+ | ` virtual ` | ✅ | ✅ | ❌ | _ not present_ | ` abstract ` <br />` override ` <br />_ not mentioned_ |
1252
+ | ` abstract ` | ✅ | ❌ | ❌ | _ not present_ <br />` virtual ` <br />` abstract ` <br />` override ` | ` abstract ` <br />` override ` <br />_ may not be<br />mentioned if<br />` D ` is not final_ |
1253
+ | ` override ` | ✅ | ✅ | ✅ | ` virtual ` <br />` abstract ` <br />` override ` | ` abstract ` <br />` override ` |
1255
1254
1256
1255
Since validating a method with a virtual modifier keyword involves looking for
1257
1256
methods with the same name in the base class, virtual methods must be declared
@@ -1315,15 +1314,15 @@ base class B1 {
1315
1314
class D1 {
1316
1315
extend base: B1;
1317
1316
// ❌ Illegal:
1318
- // impl fn F[self: Self](x: Self) -> Self;
1317
+ // override fn F[self: Self](x: Self) -> Self;
1319
1318
// since that would mean the same thing as:
1320
- // impl fn F[self: Self](x: D1) -> D1;
1319
+ // override fn F[self: Self](x: D1) -> D1;
1321
1320
// and `D1` is a different type than `B1`.
1322
1321
1323
1322
// ✅ Allowed: Parameter and return types
1324
1323
// of `F` match declaration in `B1`.
1325
- impl fn F[self: Self](x: B1) -> B1;
1326
- // Or: impl fn F[self: D1](x: B1) -> B1;
1324
+ override fn F[self: Self](x: B1) -> B1;
1325
+ // Or: override fn F[self: D1](x: B1) -> B1;
1327
1326
}
1328
1327
```
1329
1328
@@ -1341,9 +1340,9 @@ base class B2 {
1341
1340
class D2 {
1342
1341
extend base: B2;
1343
1342
// ✅ Allowed
1344
- impl fn Clone[self: Self]() -> Self*;
1343
+ override fn Clone[self: Self]() -> Self*;
1345
1344
// Means the same thing as:
1346
- // impl fn Clone[self: D2]() -> D2*;
1345
+ // override fn Clone[self: D2]() -> D2*;
1347
1346
// which is allowed since `D2*` is a
1348
1347
// subtype of `B2*`.
1349
1348
}
@@ -1615,7 +1614,7 @@ of its base classes unless it has a
1615
1614
[ virtual destructor] ( https://en.wikipedia.org/wiki/Virtual_function#Virtual_destructors ) .
1616
1615
An abstract or base class' destructor may be declared virtual using the
1617
1616
` virtual ` introducer, in which case any derived class destructor declaration
1618
- must be ` impl ` :
1617
+ must be ` override ` :
1619
1618
1620
1619
``` carbon
1621
1620
base class MyBaseClass {
@@ -1624,7 +1623,7 @@ base class MyBaseClass {
1624
1623
1625
1624
class MyDerivedClass {
1626
1625
extend base: MyBaseClass;
1627
- impl fn destroy[addr self: Self*]() { ... }
1626
+ override fn destroy[addr self: Self*]() { ... }
1628
1627
}
1629
1628
```
1630
1629
@@ -2298,6 +2297,8 @@ the type of `U.x`."
2298
2297
- [Destructor syntax options](/proposals/p5017.md#destructor-syntax-options)
2299
2298
- [Destructor name options](/proposals/p5017.md#destructor-name-options)
2300
2299
2300
+ - [#6008: Replace `impl fn` with `override fn`](https://github.com/carbon-language/carbon-lang/pull/6008)
2301
+
2301
2302
## References
2302
2303
2303
2304
- [#257: Initialization of memory and variables](https://github.com/carbon-language/carbon-lang/pull/257)
0 commit comments