@@ -14,10 +14,12 @@ Only by explicitly checking types, or when objects fail to support operations at
14
14
are the types of any values ever restricted.
15
15
16
16
Julia's type system is dynamic, but gains some of the advantages of static type systems
17
- by allowing optional type annotations. This can be of great assistance
18
- in generating efficient code, but even more significantly, it allows method dispatch on the types
19
- of function arguments to be deeply integrated with the language. Method dispatch is explored in
20
- detail in the [ Methods] ( @ref ) section, but is rooted in the type system presented here.
17
+ by allowing optional type annotations. Type annotations are important for defining efficient new data structures,
18
+ can help enforce correctness and clarity, and can sometimes improve efficiency
19
+ (although in many contexts the Julia compiler infers types automatically.
20
+ Type annotations also play a central role in Julia because they control method dispatch:
21
+ functions can be extended to new behaviours for different argument types.
22
+ Method dispatch is explored in detail in the [ Methods] ( @ref ) section, but is rooted in the type system presented here.
21
23
22
24
All values in a Julia program belong to exactly one concrete type.
23
25
All types belong to a single type tree, i.e. they are all at least related to the [ ` Any ` ] ( @ref ) type.
@@ -46,18 +48,12 @@ kinds of programming, however, become clearer, simpler, faster and more robust w
46
48
The default behavior in Julia when type annotations are omitted is to allow values to be of any type.
47
49
When additional expressiveness is needed, however,
48
50
it is easy to gradually introduce explicit annotations into previously "untyped" code.
49
- Adding annotations serves three primary purposes: to take advantage
51
+ Adding annotations serves four primary purposes: to take advantage
50
52
of Julia's powerful multiple-dispatch mechanism, to improve human readability,
51
- and to catch programmer errors.
53
+ to catch programmer errors, and to assist the compiler (especially when defining new types) .
52
54
53
55
The ` :: ` operator can be used to attach type annotations to expressions and variables in programs.
54
56
It is also used for type assertions in method signatures (see [ Defining Methods] ( #Defining-Methods ) ).
55
- There are two primary reasons to use type annotations:
56
-
57
- 1 . As an assertion to help confirm that your program works the way you expect, and
58
- 2 . To provide extra type information to the compiler, which can then improve performance in some
59
- cases.
60
-
61
57
When appended to an expression computing a value, the ` :: ` operator is read as "is an instance
62
58
of". It can be used anywhere to assert that the value of the expression on the left is an instance
63
59
of the type on the right. When the type on the right is concrete, the value on the left must have
@@ -78,7 +74,7 @@ This allows a type assertion to be attached to any expression in-place.
78
74
When the type on the right is concrete,
79
75
the value on the left must have that type as its implementation.
80
76
When the type is abstract,
81
- it suffices for the value to be implemented by a concrete type that is a subtype of the abstract type.
77
+ it suffices for the value to have a concrete type that is a subtype of the abstract type.
82
78
When appended to a variable on the left-hand side of an assignment, or as part of a ` local ` declaration,
83
79
the ` :: ` operator means something a bit different: it declares the variable to always have the
84
80
specified type, like a type declaration in a statically-typed language such as C. Every value
@@ -605,7 +601,7 @@ julia> struct Point{T}
605
601
606
602
This declaration defines a new parametric type, ` Point{T} ` ,
607
603
holding two "coordinates" of the same type ` T ` .
608
- The parameter ` T ` is a placeholder, which will be replaced by a particular concrete type
604
+ The parameter ` T ` is a placeholder, which will be replaced by a particular type
609
605
when an instance of this type is created.
610
606
Thus, this single declaration actually declares an unlimited number of types:
611
607
` Point{Float64} ` , ` Point{AbstractString} ` , ` Point{Int64} ` , etc.
0 commit comments