Skip to content

Commit 266628f

Browse files
authored
Merge pull request #3276 from ntrel/final
[spec/attribute] Document `final` attribute Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents c992eca + 54fdb68 commit 266628f

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

spec/attribute.dd

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $(GNAME Attribute):
1818
$(RELATIVE_LINK2 static, $(D static))
1919
$(RELATIVE_LINK2 linkage, $(D extern))
2020
$(RELATIVE_LINK2 abstract, $(D abstract))
21-
$(D final)
21+
$(RELATIVE_LINK2 final, `final`)
2222
$(RELATIVE_LINK2 override, $(D override))
2323
$(D synchronized)
2424
$(RELATIVE_LINK2 auto, $(D auto))
@@ -375,7 +375,8 @@ attributes in documents predating $(LINK2 http://wiki.dlang.org/DIP22, DIP22).)
375375

376376
$(P Symbols with $(D private) visibility can only be accessed from
377377
within the same module.
378-
Private member functions are implicitly final and cannot be overridden.
378+
Private member functions are implicitly $(DDSUBLINK spec/function, final, `final`)
379+
and cannot be overridden.
379380
)
380381

381382
$(P $(D package) extends private so that package members can be accessed
@@ -664,6 +665,16 @@ $(P
664665
Note that the class is still abstract and cannot be instantiated directly.
665666
)
666667

668+
$(H2 $(LNAME2 final, `final` Attribute))
669+
670+
$(UL
671+
$(LI A class can be declared $(DDSUBLINK spec/class, final, `final`) to prevent
672+
subclassing.)
673+
$(LI A class method can be declared $(DDSUBLINK spec/function, final, `final`)
674+
to prevent a derived class overriding it.)
675+
$(LI Interfaces can define $(DDSUBLINK spec/interface, method-bodies, `final` methods).)
676+
)
677+
667678
$(H2 $(LNAME2 mustuse-attribute, `@mustuse` Attribute))
668679

669680
$(P

spec/class.dd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,8 @@ final class A { }
12081208
class B : A { } // error, class A is final
12091209
---
12101210

1211-
$(P Methods of a final class are `final` by default.)
1211+
$(P Methods of a final class are always
1212+
$(DDSUBLINK spec/function, final, `final`).)
12121213

12131214
$(H2 $(LNAME2 nested, Nested Classes))
12141215

spec/function.dd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,9 @@ void test()
10121012
has its parameters changed, and all derived classes need to have
10131013
their overriding functions updated.)
10141014

1015+
$(P The $(LNAME2 final, `final`) method attribute
1016+
prevents a subclass from overriding the method.)
1017+
10151018
$(P The following are not virtual:)
10161019
$(UL
10171020
$(LI Struct and union member functions)
@@ -3730,7 +3733,7 @@ $(H2 $(LNAME2 pseudo-member, Uniform Function Call Syntax (UFCS)))
37303733
---
37313734

37323735
$(RATIONALE This provides a way to add external functions to a class as if they were
3733-
public final member functions.
3736+
public $(RELATIVE_LINK2 final, `final`) member functions.
37343737
This enables minimizing the number of functions in a class to only the essentials that
37353738
are needed to take care of the object's private state, without the temptation to add
37363739
a kitchen-sink's worth of member functions.

0 commit comments

Comments
 (0)