You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/src/manual/constructors.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ julia> foo.baz
22
22
```
23
23
24
24
For many types, forming new objects by binding their field values together is all that is ever
25
-
needed to create instances. There are, however, cases where more functionality is required when
25
+
needed to create instances. However, in some cases more functionality is required when
26
26
creating composite objects. Sometimes invariants must be enforced, either by checking arguments
27
27
or by transforming them. [Recursive data structures](https://en.wikipedia.org/wiki/Recursion_%28computer_science%29#Recursive_data_structures_.28structural_recursion.29),
28
28
especially those that may be self-referential, often cannot be constructed cleanly without first
@@ -34,7 +34,7 @@ addresses all of these cases and more.
34
34
[^1]:
35
35
Nomenclature: while the term "constructor" generally refers to the entire function which constructs
36
36
objects of a type, it is common to abuse terminology slightly and refer to specific constructor
37
-
methods as "constructors". In such situations, it is generally clear from context that the term
37
+
methods as "constructors". In such situations, it is generally clear from the context that the term
38
38
is used to mean "constructor method" rather than "constructor function", especially as it is often
39
39
used in the sense of singling out a particular method of the constructor from all of the others.
40
40
@@ -77,7 +77,7 @@ While outer constructor methods succeed in addressing the problem of providing a
77
77
methods for constructing objects, they fail to address the other two use cases mentioned in the
78
78
introduction of this chapter: enforcing invariants, and allowing construction of self-referential
79
79
objects. For these problems, one needs *inner* constructor methods. An inner constructor method
80
-
is much like an outer constructor method, with two differences:
80
+
is like an outer constructor method, except for two differences:
81
81
82
82
1. It is declared inside the block of a type declaration, rather than outside of it like normal methods.
83
83
2. It has access to a special locally existent function called [`new`](@ref) that creates objects of the
@@ -110,7 +110,7 @@ Stacktrace:
110
110
```
111
111
112
112
If the type were declared `mutable`, you could reach in and directly change the field values to
113
-
violate this invariant, but messing around with an object's internals uninvited is considered poor form.
113
+
violate this invariant. Of course, messing around with an object's internals uninvited is bad practice.
114
114
You (or someone else) can also provide additional outer constructor methods at any later point, but
115
115
once a type is declared, there is no way to add more inner constructor methods. Since outer constructor
116
116
methods can only create objects by calling other constructor methods, ultimately, some inner constructor
@@ -160,7 +160,7 @@ julia> T2(1.0)
160
160
T2(1)
161
161
```
162
162
163
-
It is considered good form to provide as few inner constructor methods as possible: only those
163
+
It is good practice to provide as few inner constructor methods as possible: only those
164
164
taking all arguments explicitly and enforcing essential error checking and transformation. Additional
165
165
convenience constructor methods, supplying default values or auxiliary transformations, should
166
166
be provided as outer constructors that call the inner constructors to do the heavy lifting. This
@@ -194,8 +194,8 @@ value for the `obj` field of another instance, such as, for example, itself.
194
194
To allow for the creation of incompletely initialized objects, Julia allows the [`new`](@ref) function
195
195
to be called with fewer than the number of fields that the type has, returning an object with
196
196
the unspecified fields uninitialized. The inner constructor method can then use the incomplete
197
-
object, finishing its initialization before returning it. Here, for example, we take another crack
198
-
at defining the `SelfReferential` type, with a zero-argument inner constructor returning instances
197
+
object, finishing its initialization before returning it. Here, for example, is another attempt
198
+
at defining the `SelfReferential` type, this time using a zero-argument inner constructor returning instances
199
199
having `obj` fields pointing to themselves:
200
200
201
201
```jldoctest selfrefer2
@@ -222,11 +222,11 @@ true
222
222
```
223
223
224
224
Although it is generally a good idea to return a fully initialized object from an inner constructor,
225
-
incompletely initialized objects can be returned:
225
+
it is possible to return incompletely initialized objects:
226
226
227
227
```jldoctest incomplete
228
228
julia> mutable struct Incomplete
229
-
xx
229
+
data
230
230
Incomplete() = new()
231
231
end
232
232
@@ -237,7 +237,7 @@ While you are allowed to create objects with uninitialized fields, any access to
237
237
reference is an immediate error:
238
238
239
239
```jldoctest incomplete
240
-
julia> z.xx
240
+
julia> z.data
241
241
ERROR: UndefRefError: access to undefined reference
242
242
```
243
243
@@ -263,13 +263,13 @@ You can pass incomplete objects to other functions from inner constructors to de
263
263
264
264
```jldoctest
265
265
julia> mutable struct Lazy
266
-
xx
266
+
data
267
267
Lazy(v) = complete_me(new(), v)
268
268
end
269
269
```
270
270
271
271
As with incomplete objects returned from constructors, if `complete_me` or any of its callees
272
-
try to access the `xx` field of the `Lazy` object before it has been initialized, an error will
272
+
try to access the `data` field of the `Lazy` object before it has been initialized, an error will
273
273
be thrown immediately.
274
274
275
275
## Parametric Constructors
@@ -513,8 +513,8 @@ it is short, self-contained, and implements an entire basic Julia type.
513
513
As we have seen, a typical parametric type has inner constructors that are called when type parameters
514
514
are known; e.g. they apply to `Point{Int}` but not to `Point`. Optionally, outer constructors
515
515
that determine type parameters automatically can be added, for example constructing a `Point{Int}`
516
-
from the call `Point(1,2)`. Outer constructors call inner constructors to do the core work of
517
-
making an instance. However, in some cases one would rather not provide inner constructors, so
516
+
from the call `Point(1,2)`. Outer constructors call inner constructors to actually
517
+
make instances. However, in some cases one would rather not provide inner constructors, so
518
518
that specific type parameters cannot be requested manually.
519
519
520
520
For example, say we define a type that stores a vector along with an accurate representation of
0 commit comments