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/types.md
+23-4Lines changed: 23 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -577,7 +577,7 @@ false
577
577
This last point is *very* important: even though `Float64 <: Real` we **DO NOT** have `Point{Float64} <: Point{Real}`.
578
578
579
579
In other words, in the parlance of type theory, Julia's type parameters are *invariant*, rather
580
-
than being covariant (or even contravariant). This is for practical reasons: while any instance
580
+
than being [covariant (or even contravariant)](https://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29). This is for practical reasons: while any instance
581
581
of `Point{Float64}` may conceptually be like an instance of `Point{Real}` as well, the two types
582
582
have different representations in memory:
583
583
@@ -603,15 +603,18 @@ function norm(p::Point{Real})
603
603
end
604
604
```
605
605
606
-
The correct way to define a method that accepts all arguments of type `Point{T}` where `T` is
606
+
A correct way to define a method that accepts all arguments of type `Point{T}` where `T` is
607
607
a subtype of `Real` is:
608
608
609
609
```julia
610
-
functionnorm{T<:Real}(p::Point{T})
610
+
functionnorm(p::Point{<:Real})
611
611
sqrt(p.x^2+ p.y^2)
612
612
end
613
613
```
614
614
615
+
(Equivalently, one could define `function norm{T<:Real}(p::Point{T})` or
616
+
`function norm(p::Point{T} where T<:Real)`; see [UnionAll Types](@ref).)
617
+
615
618
More examples will be discussed later in [Methods](@ref).
616
619
617
620
How does one construct a `Point` object? It is possible to define custom constructors for composite
0 commit comments