Skip to content

Commit 7c4fcf7

Browse files
ntrelthewilsonator
authored andcommitted
[spec/class] Improve construction docs
Make Constructors a subheading of Class Instantiation, because: * `new Object` is valid and it has no constructor * Explaining how to create a class instance should be done before the details of delegating constructors etc. * Field initialization was listed under Constructors, but applies for `new C` when there isn't a constructor. Make examples runnable. Explain when arguments to `new` are required, and how constructors are matched. Add relative link to class invariants.
1 parent e8c5dd4 commit 7c4fcf7

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

spec/class.dd

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,7 @@ $(H3 $(LNAME2 synchronized-classes, Synchronized Classes))
382382
$(NOTE struct types cannot be marked `synchronized`.)
383383

384384

385-
$(H2 $(LNAME2 constructors, Constructors))
386-
387-
$(GRAMMAR
388-
$(GNAME Constructor):
389-
$(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
390-
$(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, MissingFunctionBody)
391-
$(GLINK2 template, ConstructorTemplate)
392-
)
385+
$(H2 $(LNAME2 class-instantiation, Class Instantiation))
393386

394387
$(P Fields are by default initialized to the
395388
$(DDSUBLINK spec/property, init, default initializer)
@@ -398,34 +391,74 @@ $(GNAME Constructor):
398391
If the field declaration has an optional $(GLINK2 declaration, Initializer)
399392
that will be used instead of the default.
400393
)
394+
395+
$(SPEC_RUNNABLE_EXAMPLE_RUN
401396
------
402397
class Abc
403398
{
404399
int a; // default initializer for a is 0
405400
long b = 7; // default initializer for b is 7
406401
float f; // default initializer for f is NAN
407402
}
403+
404+
void main()
405+
{
406+
Abc obj = new Abc;
407+
assert(obj.b == 7);
408+
}
408409
------
410+
)
409411

410412
$(P The $(I Initializer) is evaluated at compile time.)
411413

412-
$(P This initialization is done before any constructors are
413-
called.)
414+
$(P This initialization is done before any
415+
$(RELATIVE_LINK2 constructors, constructors) are called.)
416+
417+
$(P Instances of class objects are created with a $(GLINK2 expression, NewExpression).)
418+
419+
* A class `C` without a constructor (or with a nullary constructor) is instantiated
420+
using `new C`, without passing any arguments.
421+
* Otherwise, a class can be instantiated with an argument list, e.g.
422+
`new C(arguments)`. The arguments are passed to a constructor with a matching
423+
parameter list (and resolved like $(DDSUBLINK spec/function, function-overloading,
424+
overloaded methods)).
425+
426+
$(P By default, a class instance is allocated on the garbage-collected heap.
427+
A $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance)
428+
is allocated on the stack.)
429+
430+
431+
$(H3 $(LNAME2 constructors, Constructors))
432+
433+
$(GRAMMAR
434+
$(GNAME Constructor):
435+
$(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
436+
$(D this) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, MissingFunctionBody)
437+
$(GLINK2 template, ConstructorTemplate)
438+
)
414439

415440
$(P Constructors are defined with a function name of $(D this)
416441
and have no return value:)
417442

443+
$(SPEC_RUNNABLE_EXAMPLE_RUN
418444
------
419-
class Foo
445+
class A
420446
{
421-
$(CODE_HIGHLIGHT this)(int x) // declare constructor for Foo
422-
{ ...
423-
}
424-
$(CODE_HIGHLIGHT this)()
425-
{ ...
447+
int i;
448+
449+
this(int i) // constructor taking an int argument
450+
{
451+
this.i = i; // initialize field `i` from parameter `i`
426452
}
427453
}
454+
455+
void main()
456+
{
457+
A a = new A(3);
458+
assert(a.i == 3);
459+
}
428460
------
461+
)
429462

430463
$(H3 $(LNAME2 base-construction, Base Class Construction))
431464

@@ -528,16 +561,7 @@ $(H3 $(LNAME2 implicit-base-construction, Implicit Base Class Construction))
528561
base class has a constructor that requires arguments and no
529562
nullary constructor, a matching call to `super` is required.)
530563

531-
$(H3 $(LNAME2 class-instantiation, Class Instantiation))
532-
533-
$(P Instances of class objects are created with a $(GLINK2 expression, NewExpression):)
534-
535-
------
536-
A a = new A(3);
537-
------
538-
539-
$(P A $(DDSUBLINK spec/attribute, scope-class-var, `scope` class instance)
540-
is allocated on the stack.)
564+
$(H3 $(LNAME2 instantiation-process, Instantiation Process))
541565

542566
$(P The following steps happen:)
543567

@@ -568,8 +592,8 @@ $(H3 $(LNAME2 class-instantiation, Class Instantiation))
568592

569593
$(LI The body of the constructor is executed.)
570594

571-
$(LI If class invariant checking is turned on, the class invariant
572-
is called at the end of the constructor.
595+
$(LI If class invariant checking is turned on, the
596+
$(RELATIVE_LINK2 invariants, class invariant) is called at the end of the constructor.
573597
)
574598
)
575599

0 commit comments

Comments
 (0)