4
4
Constructors
5
5
**************
6
6
7
- Constructors are functions that create new objects — specifically,
7
+ Constructors [ # ]_ are functions that create new objects — specifically,
8
8
instances of :ref: `man-composite-types `. In Julia,
9
9
type objects also serve as constructor functions: they create new
10
10
instances of themselves when applied to an argument tuple as a function.
@@ -39,6 +39,15 @@ construct objects with fewer or different types of parameters than they
39
39
have fields. Julia's system for object construction addresses all of
40
40
these cases and more.
41
41
42
+ .. [# ] Nomenclature: while the term “constructor” generally refers to
43
+ the entire function which constructs objects of a type, it is common to
44
+ abuse terminology slightly and refer to specific constructor methods as
45
+ “constructors”. In such situations, it is generally clear from context
46
+ that the term is used to mean “constructor method” rather than
47
+ “constructor function”, especially as it is often used in the sense of
48
+ singling out a particular method of the constructor from all of the
49
+ others.
50
+
42
51
Outer Constructor Methods
43
52
-------------------------
44
53
@@ -71,23 +80,6 @@ this are called *outer* constructor methods. Outer constructor methods
71
80
can only ever create a new instance by calling another constructor
72
81
method, such as the automatically provided default one.
73
82
74
- .. raw :: html
75
-
76
- <div class =" sidebar" >
77
-
78
- A Note On Nomenclature. While the term “constructor” generally refers to
79
- the entire function which constructs objects of a type, it is common to
80
- abuse terminology slightly and refer to specific constructor methods as
81
- “constructors”. In such situations, it is generally clear from context
82
- that the term is used to mean “constructor method” rather than
83
- “constructor function”, especially as it is often used in the sense of
84
- singling out a particular method of the constructor from all of the
85
- others.
86
-
87
- .. raw :: html
88
-
89
- </div >
90
-
91
83
Inner Constructor Methods
92
84
-------------------------
93
85
@@ -178,11 +170,9 @@ with an explicit constructor::
178
170
179
171
julia> T1(1.0)
180
172
no method T1(Float64,)
181
- in method_missing at /Users/stefan/projects/julia/base/base.jl:58
182
173
183
174
julia> T2(1.0)
184
175
no method T2(Float64,)
185
- in method_missing at /Users/stefan/projects/julia/base/base.jl:58
186
176
187
177
It is considered good form to provide as few inner constructor methods
188
178
as possible: only those taking all arguments explicitly and enforcing
@@ -307,7 +297,6 @@ types of the arguments given to the constructor. Here are some examples::
307
297
308
298
julia> Point(1,2.5)
309
299
no method Point(Int64,Float64)
310
- in method_missing at /Users/stefan/projects/julia/base/base.jl:58
311
300
312
301
## explicit T ##
313
302
@@ -316,14 +305,12 @@ types of the arguments given to the constructor. Here are some examples::
316
305
317
306
julia> Point{Int64}(1.0,2.5)
318
307
no method Point(Float64,Float64)
319
- in method_missing at /Users/stefan/projects/julia/base/base.jl:58
320
308
321
309
julia> Point{Float64}(1.0,2.5)
322
310
Point(1.0,2.5)
323
311
324
312
julia> Point{Float64}(1,2)
325
313
no method Point(Int64,Int64)
326
- in method_missing at /Users/stefan/projects/julia/base/base.jl:58
327
314
328
315
As you can see, for constructor calls with explicit type parameters, the
329
316
arguments must match that specific type: ``Point{Int64}(1,2) `` works,
0 commit comments