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
docs: Clarify important points about parametric types
ClosesJuliaLang#43811.
- The intro section felt a bit long-winded, I've made some changes there first.
- Clarify that `typeof` returns the concrete type
- Rename Type Declarations section to Type Annotations,
avoid confusion with Declaring Types section and distinguish use of "type declaration"
to mean "declaring new types"
- Removed some of the jargon and wikipedia links to make room for
a brief alternative `Point{T1,T2}` demonstration.
- Shifted some paragraphs around to reflect their importance,
and changed the wording in some places.
- Rename type declaration -> annotation in other places (docstrings/comments)
Copy file name to clipboardexpand all lines: doc/src/index.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -79,13 +79,13 @@ The most significant departures of Julia from typical dynamic languages are:
79
79
* The core language imposes very little; Julia Base and the standard library are written in Julia itself, including
80
80
primitive operations like integer arithmetic
81
81
* A rich language of types for constructing and describing objects, that can also optionally be
82
-
used to make type declarations
82
+
used to write type annotations
83
83
* The ability to define function behavior across many combinations of argument types via [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch)
84
84
* Automatic generation of efficient, specialized code for different argument types
85
85
* Good performance, approaching that of statically-compiled languages like C
86
86
87
-
Although one sometimes speaks of dynamic languages as being "typeless", they are definitely not.
88
-
Every object, whether primitive or user-defined, has a type. The lack of type declarations in
87
+
Although one sometimes speaks of dynamic languages as being "typeless", they are definitely not:
88
+
every object, whether primitive or user-defined, has a type. The lack of type annotations in
89
89
most dynamic languages, however, means that one cannot instruct the compiler about the types of
90
90
values, and often cannot explicitly talk about types at all. In static languages, on the other
91
91
hand, while one can -- and usually must -- annotate types for the compiler, types exist only at
and the `::Integer` specification means that it will only be callable when `n` is a subtype of the [abstract](@ref man-abstract-types) `Integer` type.
114
114
115
-
Argument-type declarations**normally have no impact on performance**: regardless of what argument types (if any) are declared, Julia compiles a specialized version of the function for the actual argument types passed by the caller. For example, calling `fib(1)` will trigger the compilation of specialized version of `fib` optimized specifically for `Int` arguments, which is then re-used if `fib(7)` or `fib(15)` are called. (There are rare exceptions when an argument-type declaration can trigger additional compiler specializations; see: [Be aware of when Julia avoids specializing](@ref).) The most common reasons to declare argument types in Julia are, instead:
115
+
Argument-type annotations**normally have no impact on performance**: regardless of what argument types (if any) are declared, Julia compiles a specialized version of the function for the actual argument types passed by the caller. For example, calling `fib(1)` will trigger the compilation of specialized version of `fib` optimized specifically for `Int` arguments, which is then re-used if `fib(7)` or `fib(15)` are called. (There are rare exceptions when an argument-type annotation can trigger additional compiler specializations; see: [Be aware of when Julia avoids specializing](@ref).) The most common reasons to declare argument types in Julia are, instead:
116
116
117
117
***Dispatch:** As explained in [Methods](@ref), you can have different versions ("methods") of a function for different argument types, in which case the argument types are used to determine which implementation is called for which arguments. For example, you might implement a completely different algorithm `fib(x::Number) = ...` that works for any `Number` type by using [Binet's formula](https://en.wikipedia.org/wiki/Fibonacci_number#Binet%27s_formula) to extend it to non-integer values.
118
-
***Correctness:** Type declarations can be useful if your function only returns correct results for certain argument types. For example, if we omitted argument types and wrote `fib(n) = n ≤ 2 ? one(n) : fib(n-1) + fib(n-2)`, then `fib(1.5)` would silently give us the nonsensical answer `1.0`.
119
-
***Clarity:** Type declarations can serve as a form of documentation about the expected arguments.
118
+
***Correctness:** Type annotations can be useful if your function only returns correct results for certain argument types. For example, if we omitted argument types and wrote `fib(n) = n ≤ 2 ? one(n) : fib(n-1) + fib(n-2)`, then `fib(1.5)` would silently give us the nonsensical answer `1.0`.
119
+
***Clarity:** Type annotations can serve as a form of documentation about the expected arguments.
120
120
121
121
However, it is a **common mistake to overly restrict the argument types**, which can unnecessarily limit the applicability of the function and prevent it from being re-used in circumstances you did not anticipate. For example, the `fib(n::Integer)` function above works equally well for `Int` arguments (machine integers) and `BigInt` arbitrary-precision integers (see [BigFloats and BigInts](@ref BigFloats-and-BigInts)), which is especially useful because Fibonacci numbers grow exponentially rapidly and will quickly overflow any fixed-precision type like `Int` (see [Overflow behavior](@ref)). If we had declared our function as `fib(n::Int)`, however, the application to `BigInt` would have been prevented for no reason. In general, you should use the most general applicable abstract types for arguments, and **when in doubt, omit the argument types**. You can always add argument-type specifications later if they become necessary, and you don't sacrifice performance or functionality by omitting them.
122
122
@@ -201,9 +201,9 @@ Int8
201
201
```
202
202
203
203
This function will always return an `Int8` regardless of the types of `x` and `y`.
204
-
See [Type Declarations](@ref) for more on return types.
204
+
See [Type Annotations](@ref) for more on return types.
205
205
206
-
Return type declarations are **rarely used** in Julia: in general, you should
206
+
Return type annotations are **rarely used** in Julia: in general, you should
207
207
instead write "type-stable" functions in which Julia's compiler can automatically
208
208
infer the return type. For more information, see the [Performance Tips](@ref man-performance-tips) chapter.
0 commit comments