Skip to content

Commit

Permalink
Add dispatch on parametric types
Browse files Browse the repository at this point in the history
(cherry picked from commit 06b52dd)
ref #13453
  • Loading branch information
matthieugomez authored and tkelman committed Oct 7, 2015
1 parent 50dfceb commit 7048c40
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/manual/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,21 @@ pointers to individually allocated :obj:`Real` objects — which may well be
complex objects, which are declared to be implementations of the
:obj:`Real` abstract type.

Since ``Point{Float64}`` is not a subtype of ``Point{Real}``, the following method can't be applied to arguments of type ``Point{Float64}``::

function norm(p::Point{Real})
sqrt(p.x^2 + p.y^2)
end

The correct way to define a method that accepts all arguments of type ``Point{T}`` where ``T`` is a subtype of ``Real`` is::

function norm{T<:Real}(p::Point{T})
sqrt(p.x^2 + p.y^2)
end

More examples will be discussed later in :ref:`man-methods`.


How does one construct a ``Point`` object? It is possible to define
custom constructors for composite types, which will be discussed in
detail in :ref:`man-constructors`, but in the absence of any
Expand Down

0 comments on commit 7048c40

Please sign in to comment.