Skip to content

Commit 6b43013

Browse files
committed
Make class casting example runnable
1 parent 259ebc7 commit 6b43013

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

spec/expression.dd

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,18 +1036,24 @@ $(H3 $(LNAME2 cast_class, Class References))
10361036
really is a downcast. $(D null) is the result if it isn't.
10371037
)
10381038

1039+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10391040
-------------
1040-
class A { ... }
1041-
class B : A { ... }
1041+
class A {}
1042+
class B : A {}
10421043

1043-
void test(A a, B b)
1044+
void main()
10441045
{
1045-
B bx = a; // error, need cast
1046-
B bx = cast(B) a; // bx is null if a is not a B
1047-
A ax = b; // no cast needed
1048-
A ax = cast(A) b; // no runtime check needed for upcast
1046+
A a = new A;
1047+
//B b = a; // error, need cast
1048+
B b = cast(B) a; // b is null if a is not a B
1049+
assert(b is null);
1050+
1051+
a = b; // no cast needed
1052+
a = cast(A) b; // no runtime check needed for upcast
1053+
assert(a is b);
10491054
}
10501055
-------------
1056+
)
10511057

10521058
$(P In order to determine if an object $(D o) is an instance of
10531059
a class $(D B) use a cast:)

0 commit comments

Comments
 (0)