Skip to content

Commit 3eb0987

Browse files
authored
[spec/declaration] Tweak Storage Class docs (#3554)
* [spec/declaration] Tweak Storage Class docs Add links. Make examples runnable. Use distinct parameter name vs argument name. Add 2 subheadings. * Add links for const attribute * Fix missing `)`, remove redundant MULTICOLS
1 parent 00887fa commit 3eb0987

File tree

2 files changed

+60
-35
lines changed

2 files changed

+60
-35
lines changed

spec/attribute.dd

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ $(GNAME Attribute):
3131
$(RELATIVE_LINK2 scope, $(D scope))
3232
$(RELATIVE_LINK2 shared, $(D shared))
3333
$(RELATIVE_LINK2 static, $(D static))
34-
$(D synchronized)
34+
$(DDSUBLINK spec/class, synchronized-classes, `synchronized`)
3535

3636
$(GNAME FunctionAttributeKwd):
3737
$(RELATIVE_LINK2 nothrow, $(D nothrow))
@@ -463,7 +463,8 @@ $(H3 $(LNAME2 export, $(D export) Attribute))
463463

464464
$(H2 $(LNAME2 const, $(D const) Attribute))
465465

466-
$(P The $(D const) attribute changes the type of the declared symbol from $(D T) to $(D const(T)),
466+
$(P The $(DDLINK spec/const3, Type Qualifiers, $(D const) type qualifier)
467+
changes the type of the declared symbol from $(D T) to $(D const(T)),
467468
where $(D T) is the type specified (or inferred) for the introduced symbol in the absence of $(D const).
468469
)
469470

@@ -474,7 +475,10 @@ $(H2 $(LNAME2 const, $(D const) Attribute))
474475

475476
const double bar = foo + 6;
476477
static assert(is(typeof(bar) == const(double)));
477-
478+
---
479+
)
480+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
481+
---
478482
class C
479483
{
480484
const void foo();
@@ -487,11 +491,14 @@ $(H2 $(LNAME2 const, $(D const) Attribute))
487491
pragma(msg, typeof(C.foo)); // const void()
488492
pragma(msg, typeof(C.bar)); // const void()
489493
pragma(msg, typeof(C.baz)); // const void()
494+
490495
static assert(is(typeof(C.foo) == typeof(C.bar)) &&
491496
is(typeof(C.bar) == typeof(C.baz)));
492497
---------------
493498
)
494499

500+
$(P See also: $(DDSUBLINK spec/declaration, methods-returning-qualified, Methods Returning a Qualified Type).)
501+
495502
$(H2 $(LNAME2 immutable, $(D immutable) Attribute))
496503

497504
$(P The $(D immutable) attribute modifies the type from $(D T) to $(D immutable(T)),
@@ -593,11 +600,12 @@ $(H2 $(LNAME2 pure, $(D pure) Attribute))
593600

594601
$(H2 $(LNAME2 ref, $(D ref) Attribute))
595602

596-
$(P See $(DDSUBLINK spec/function, ref-functions, Ref Functions).)
603+
$(P See $(DDSUBLINK spec/declaration, ref-storage, `ref` Storage Class).)
597604

598605
$(H2 $(LNAME2 return, $(D return) Attribute))
599606

600-
$(P See $(DDSUBLINK spec/function, return-ref-parameters, Return Ref Parameters).)
607+
* $(DDSUBLINK spec/function, return-ref-parameters, Return Ref Parameters).
608+
* $(DDSUBLINK spec/function, return-scope-parameters, Return Scope Parameters).
601609

602610
$(H2 $(LNAME2 override, $(D override) Attribute))
603611

spec/declaration.dd

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,36 @@ $(GNAME VarDeclarator):
6565

6666
$(H3 $(LNAME2 storage-classes, Storage Classes))
6767

68+
$(P See $(RELATIVE_LINK2 typequal_vs_storageclass, Type Classes vs. Storage Classes).)
69+
6870
$(GRAMMAR
6971
$(GNAME StorageClasses):
7072
$(GLINK StorageClass)
7173
$(GLINK StorageClass) $(GSELF StorageClasses)
7274

7375
$(GNAME StorageClass):
74-
$(MULTICOLS 5, $(GLINK2 attribute, LinkageAttribute)
76+
$(GLINK2 attribute, LinkageAttribute)
7577
$(GLINK2 attribute, AlignAttribute)
7678
$(GLINK2 attribute, AtAttribute)
77-
$(D deprecated)
78-
$(D enum)
79-
$(D static)
79+
$(DDSUBLINK spec/attribute, deprecated, `deprecated`)
80+
$(DDSUBLINK spec/enum, manifest_constants, `enum`)
81+
$(DDSUBLINK spec/attribute, static, `static`)
8082
$(RELATIVE_LINK2 extern, $(D extern))
81-
$(D abstract)
82-
$(D final)
83-
$(D override)
84-
$(D synchronized)
85-
$(D auto)
86-
$(D scope)
87-
$(D const)
88-
$(D immutable)
89-
$(D inout)
90-
$(D shared)
91-
$(D __gshared)
83+
$(DDSUBLINK spec/class, abstract, `abstract`)
84+
$(DDSUBLINK spec/class, final, `final`)
85+
$(DDSUBLINK spec/function, virtual-functions, `override`)
86+
$(DDSUBLINK spec/class, synchronized-classes, `synchronized`)
87+
$(RELATIVE_LINK2 auto-declaration, `auto`)
88+
$(DDSUBLINK spec/attribute, scope, `scope`)
89+
$(DDLINK spec/const3, Type Qualifiers, `const`)
90+
$(DDLINK spec/const3, Type Qualifiers, `immutable`)
91+
$(DDSUBLINK spec/const3, inout, `inout`)
92+
$(DDSUBLINK spec/const3, shared, `shared`)
93+
$(DDSUBLINK spec/attribute, gshared, `__gshared`)
9294
$(GLINK2 attribute, Property)
93-
$(D nothrow)
94-
$(D pure)
95-
$(D ref))
95+
$(DDSUBLINK spec/function, nothrow-functions, `nothrow`)
96+
$(DDSUBLINK spec/function, pure-functions, `pure`)
97+
$(RELATIVE_LINK2 ref-storage, `ref`)
9698
)
9799

98100
$(H3 $(LNAME2 initializers, Initializers))
@@ -664,7 +666,8 @@ $(H2 $(LNAME2 global_static_init, Global and Static Initializers))
664666

665667
$(H2 $(LNAME2 typequal_vs_storageclass, Type Qualifiers vs. Storage Classes))
666668

667-
$(P Type qualifer and storage classes are distinct.)
669+
$(P $(DDLINK spec/const3, Type Qualifiers, Type qualifers) and
670+
$(RELATIVE_LINK2 storage-classes, storage classes) are distinct concepts.)
668671

669672
$(P A $(I type qualifier) creates a derived type from an existing base
670673
type, and the resulting type may be used to create multiple instances
@@ -689,6 +692,7 @@ ImmutableInt z; // typeof(z) == immutable(int)
689692
with the $(D const) storage class to indicate that it does not modify
690693
its implicit $(D this) argument:)
691694

695+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
692696
--------
693697
struct S
694698
{
@@ -700,16 +704,22 @@ struct S
700704
}
701705
}
702706
--------
703-
704-
$(P Although some keywords can be used both as a type qualifier and a
707+
)
708+
$(P Although some keywords can be
709+
$(RELATIVE_LINK2 methods-returning-qualified, used as both) a type qualifier and a
705710
storage class, there are some storage classes that cannot be used to
706-
construct new types, such as $(D ref):)
711+
construct new types, such as $(D ref).)
712+
713+
$(H3 $(LNAME2 ref-storage, `ref` Storage Class))
714+
715+
$(P A parameter $(DDSUBLINK spec/function, ref-params, declared with `ref`)
716+
is passed by reference:)
707717

718+
$(SPEC_RUNNABLE_EXAMPLE_RUN
708719
--------
709-
// ref declares the parameter x to be passed by reference
710-
void func(ref int x)
720+
void func(ref int i)
711721
{
712-
x++; // so modifications to x will be visible in the caller
722+
i++; // modifications to i will be visible in the caller
713723
}
714724

715725
void main()
@@ -719,12 +729,14 @@ void main()
719729
assert(x == 2);
720730

721731
// However, ref is not a type qualifier, so the following is illegal:
722-
ref(int) y; // Error: ref is not a type qualifier.
732+
//ref(int) y; // Error: ref is not a type qualifier.
723733
}
724734
--------
735+
)
736+
$(P Functions can also be $(DDSUBLINK spec/function, ref-functions, declared as `ref`),
737+
meaning their return value is passed by reference:)
725738

726-
$(P Functions can also be declared as `ref`, meaning their return value is
727-
passed by reference:)
739+
$(SPEC_RUNNABLE_EXAMPLE_RUN
728740
--------
729741
ref int func2()
730742
{
@@ -749,11 +761,14 @@ void main()
749761
// does not inherit the ref storage class from func2().
750762
}
751763
--------
764+
)
765+
$(H3 $(LNAME2 methods-returning-qualified, Methods Returning a Qualified Type))
752766

753767
$(P Some keywords, such as $(D const), can be used
754768
both as a type qualifier and a storage class.
755769
The distinction is determined by the syntax where it appears.)
756770

771+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
757772
---
758773
struct S
759774
{
@@ -763,8 +778,9 @@ void main()
763778
*/
764779
const int* func() // a const function
765780
{
766-
++p; // error, this.p is const
767-
return p; // error, cannot convert const(int)* to int*
781+
//++p; // error, this.p is const
782+
//return p; // error, cannot convert const(int)* to int*
783+
return null;
768784
}
769785

770786
const(int)* func() // a function returning a pointer to a const int
@@ -776,6 +792,7 @@ void main()
776792
int* p;
777793
}
778794
---
795+
)
779796

780797
$(BEST_PRACTICE To avoid confusion, the type qualifier
781798
syntax with parentheses should be used for return types,

0 commit comments

Comments
 (0)