Skip to content

Commit 64b8149

Browse files
ntreldlang-bot
authored andcommitted
[spec/type] Improve typeof docs
Explain usage, mention *PrimaryExpression*. Make example compilable & extend. Mention `typeof(null)`. Add subheading for special cases.
1 parent d5ecc34 commit 64b8149

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

spec/type.dd

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -762,42 +762,64 @@ $(GNAME Typeof):
762762
)
763763

764764
$(P
765-
$(D typeof) is a way to specify a type based on the type
766-
of an expression. For example:
765+
The first form gives the type of an expression.
766+
It can be used anywhere a $(GLINK BasicType) is expected, such as in a
767+
declaration. It is also useful as a $(GLINK2 expression, PrimaryExpression)
768+
in a sub-expression. For example:
767769
)
768770

771+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
769772
--------------------
770773
void func(int i)
771774
{
772775
typeof(i) j; // j is of type int
773776
typeof(3 + 6.0) x; // x is of type double
774-
typeof(1)* p; // p is of type pointer to int
775-
int[typeof(p)] a; // a is of type int[int*]
776777

777-
writeln(typeof('c').sizeof); // prints 1
778-
double c = cast(typeof(1.0))j; // cast j to double
778+
// as part of a derived type:
779+
typeof(1)* p;
780+
static assert(is(typeof(p) == int*));
781+
typeof(p)[int] aa;
782+
static assert(is(typeof(aa) == int*[int]));
783+
784+
auto d = cast(typeof(1.0)) i; // cast i to double
785+
static assert(is(typeof(d) == double));
786+
787+
// as a sub-expression:
788+
static assert(typeof('c').sizeof == 1); // char.sizeof
789+
Exception[2] sa;
790+
Exception ex = new typeof(sa[0])("message"); // new Exception("message")
779791
}
780792
--------------------
793+
)
781794

782795
$(P
783796
$(I Expression) is not evaluated, it is used purely to
784797
generate the type:
785798
)
786799

800+
$(SPEC_RUNNABLE_EXAMPLE_RUN
787801
--------------------
788-
void func()
802+
void main()
789803
{
790804
int i = 1;
791805
typeof(++i) j; // j is declared to be an int, i is not incremented
792-
writeln(i); // prints 1
806+
assert(i == 1);
793807
}
794808
--------------------
809+
)
795810

796811
$(P If *Expression* is a
797-
$(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq))
798-
it will produce a *TypeSeq* containing the types of each element.)
812+
$(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq)), `typeof`
813+
will produce a *TypeSeq* containing the types of each element.)
814+
815+
$(P `typeof(null)` is useful to get the type of the
816+
$(DDSUBLINK spec/expression, null, `null`) literal.)
817+
818+
$(BEST_PRACTICE $(I Typeof) is most useful in writing generic
819+
template code.)
820+
821+
$(H3 $(LNAME2 typeof-special, Special Cases))
799822

800-
$(P Special cases: )
801823
$(OL
802824
$(LI $(D typeof(return)) will, when inside a function scope,
803825
give the return type of that function.
@@ -851,12 +873,6 @@ $(GNAME Typeof):
851873
static assert(is(typeof(t) == void));
852874
--------------------
853875

854-
$(BEST_PRACTICE
855-
$(OL
856-
$(LI $(I Typeof) is most useful in writing generic
857-
template code.)
858-
)
859-
)
860876

861877
$(H2 $(LNAME2 mixin_types, Mixin Types))
862878

0 commit comments

Comments
 (0)