Language Devel / DIPs /
DIP10
DIP10: Qualified constructors and destructors for structs
*** Work in progress ***
|
Abstract
This is a design Walter and I conceived a long time ago but he never got around to implementing it. Because of this delay many structs are near unusable with qualifier. In brief, constructors and destructors are overloadable on qualifiers.
Rationale
Creating immutable objects must observe quite different (and more strict) rules than creating mutable objects.
Description
Copy construction of qualified structs
If a struct does not define this(this), bitblitting works for copying around objects of the same type and same qualifier. Also, bitblitting works for copying a mutable or immutable object into a const one. In addition, a reference to a mutable or immutable object can be always implicitly converted to a reference to a const object.
If a struct does not define this(this) _and_ has no mutable aliasing, bitblitting converts from any qualifier to any other qualifier.
If a struct defines this(this) _and_ has no mutable aliasing, that copy constructor will be called to copy a qualified object to an object of a different qualifier. Example:
|
If a type with mutable aliasing defines this(this), the rules are more restrictive. That constructor can only copy unqualified objects. To copy qualified objects, qualified copy constructors may be defined:
|
To copy an object with mutable aliasing from an object with the same type but different qualifiers, just define a regular constructor with the appropriate qualifications:
|
This example also illustrates that conversion to const is implicit if not defined: a mutable or immutable object can be converted to a const object by means of bitblitting. The latter copy in the example involves a copy from const because the conversion to const is implicit.
Qualified constructors
Structs should allow defining qualified constructors and destructors:
|
The typechecking rules for the const constructor as as follows:
- Fields can be assigned. The source of the assignment must be a const-qualified type of the field.
- Each field can be assigned at most once.
- Otherwise the constructor obeys normal const typechecking.
- Fields can be assigned. The source of the assignment must be a const-qualified type of the field.
- Each field can be assigned at most once.
- Otherwise the constructor obeys normal const (sic) typechecking.
The assignments are considered calls to the copy constructors of the fields. Fields that are not assigned to during construction are left initialized with their .init value.
Qualified destructors are typed like ordinary qualified methods.
Aliasing
We distinguish two kinds of structs: with or without mutable aliasing. A struct has mutable aliasing if mutable data can be reached outside the struct's storage by following fields of that struct. Example of a struct with mutable aliasing:
|
Mutable data is reachable starting from the field "next". Similar examples include fields of type int[] or class type. An example of a struct without mutable aliasing:
|
A less obvious example of a struct without mutable aliasing consists of structs with fields referring to immutable data. For example, the struct below also doesn't feature mutable aliasing:
|
Although Widget contains aliases to data outside of itself, the reachable data is not modifiable.
The compiler statically knows for any struct type whether it has mutable aliasing or not. The behavior of qualified constructors and destructors depend on that trait as follows.
Structs without mutable aliasing
Such types don't need to define const and/or immutable constructors. This is because it suffice to copy the bits of an object to obtain a qualified object. Consider:
|
To obtain a const Point, creating a Point and then copying its bits will suffice. Note that it's important to copy the bits, otherwise undue aliasing may occur. Example:
|
If the bits aren't copied, then global would hold a mutable alias to an immutable value.
Copy construction of qualified objects
A struct may define
Examples
Copyright
This document has been placed in the Public Domain.