Fonctions (Functions)

Déclarations d'opérateurs
# ⊔ est obtenu  qcup
# see https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm to get operators in Julia
Base.isoperator(:⊔)
# opérateur défini comme une fonction
⊔(a,b) = a + b
⊔(a::Int, b::Int) = a * b
23.0 # ou ⊔(2,3.0)
23 # ou ⊔(2,3)
# opérateur défini comme un opérateur (pas sûr d'être dans la doc)
# ▿ obtenu avec 	riangledown   
Base.isoperator(:→)
a → b = a + b
a::Int → b::Int = a * b
23.0 # ou →(2,3.0)
23 # ou →(2,3)
julia> # ⊔ est obtenu  qcup
julia> # see https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm to get operators in Julia
julia> Base.isoperator(:⊔)
true
julia> # opérateur défini comme une fonction
julia> ⊔(a,b) = a + b
⊔ (generic function with 2 methods)
julia> ⊔(a::Int, b::Int) = a * b
⊔ (generic function with 2 methods)
julia> 23.0 # ou ⊔(2,3.0)
5.0
julia> 23 # ou ⊔(2,3)
6
julia> # opérateur défini comme un opérateur (pas sûr d'être dans la doc)
julia> # ▿ obtenu avec 	riangledown   
julia> Base.isoperator(:→)
true
julia> a → b = a + b
→ (generic function with 2 methods)
julia> a::Int → b::Int = a * b
→ (generic function with 2 methods)
julia> 23.0 # ou →(2,3.0)
5.0
julia> 23 # ou →(2,3)
6

Méthodes (Methods)

Les Types

En julia, on peut être surpris par des déclarations de fonctions/méthodes de la forme f(::Type{Float64}, x::Int) ou f(x::T, v::Vector{T}) where {T}.

Les types de types

Type de Type
function g(x, typ::Type{Float64})
    if x isa typ
        "c'est bien un réel"
    else
        "ce n'est pas un réel"
    end
end

function g(x, typ::Type{Int64})
    if x isa typ
        "c'est bien un entier"
    else
        "ce n'est pas un entier"
    end
end

## Comme la valeur de typ est constant 
## pas besoin de le spécifier 
## et utiliser sa valeur à l'intérieur de la méthode
function f(x, ::Type{Float64})
    if x isa Float64
        "c'est bien un réel"
    else
        "ce n'est pas un réel"
    end
end

function f(x, ::Type{Int64})
    if x isa Int64
        "c'est bien un entier"
    else
        "ce n'est pas un entier"
    end
end

g(1, Float64)
g(1.0, Float64)
g(1, Int64)
g(1.0, Int64)

f(1, Float64)
f(1.0, Float64)
f(1, Int64)
f(1.0, Int64)

Float64 isa Type{Float64}
Int64 isa Type{Int64}

typeof(Float64)
typeof(Int64)

Float64 isa DataType
Float64 isa Type

supertype(Type{Float64})
supertypes(Type{Float64})
Type{Float64} <: DataType
julia> function g(x, typ::Type{Float64})
           if x isa typ
               "c'est bien un réel"
           else
               "ce n'est pas un réel"
           end
       end
g (generic function with 2 methods)
julia> 
julia> function g(x, typ::Type{Int64})
           if x isa typ
               "c'est bien un entier"
           else
               "ce n'est pas un entier"
           end
       end
g (generic function with 2 methods)
julia> 
julia> ## Comme la valeur de typ est constant 
julia> ## pas besoin de le spécifier 
julia> ## et utiliser sa valeur à l'intérieur de la méthode
julia> function f(x, ::Type{Float64})
           if x isa Float64
               "c'est bien un réel"
           else
               "ce n'est pas un réel"
           end
       end
f (generic function with 2 methods)
julia> 
julia> function f(x, ::Type{Int64})
           if x isa Int64
               "c'est bien un entier"
           else
               "ce n'est pas un entier"
           end
       end
f (generic function with 2 methods)
julia> 
julia> g(1, Float64)
"ce n'est pas un réel"
julia> g(1.0, Float64)
"c'est bien un réel"
julia> g(1, Int64)
"c'est bien un entier"
julia> g(1.0, Int64)
"ce n'est pas un entier"
julia> 
julia> f(1, Float64)
"ce n'est pas un réel"
julia> f(1.0, Float64)
"c'est bien un réel"
julia> f(1, Int64)
"c'est bien un entier"
julia> f(1.0, Int64)
"ce n'est pas un entier"
julia> 
julia> Float64 isa Type{Float64}
true
julia> Int64 isa Type{Int64}
true
julia> 
julia> typeof(Float64)
DataType
julia> typeof(Int64)
DataType
julia> 
julia> Float64 isa DataType
true
julia> Float64 isa Type
true
julia> 
julia> supertype(Type{Float64})
Any
julia> supertypes(Type{Float64})
(Type{Float64}, Any)
julia> Type{Float64} <: DataType
true

Les types et méthodes paramétriques (type UnionAll)

Types UnionAll
Complex isa UnionAll
Vector isa UnionAll

Vector == Array{1}
Vector == Array{T,1} where {T}
Vector == Vector{T} where {T}
Vector{<:Real} == Vector{T} where T<:Real
Vector{Int64} <: Vector
Vector{Int64} <: Vector{<:Real}
## Mais attention!
Int64 <: Integer, Vector{Int64} <: Vector{Integer}
julia> Complex isa UnionAll
true
julia> Vector isa UnionAll
true
julia> 
julia> Vector == Array{1}
false
julia> Vector == Array{T,1} where {T}
true
julia> Vector == Vector{T} where {T}
true
julia> Vector{<:Real} == Vector{T} where T<:Real
true
julia> Vector{Int64} <: Vector
true
julia> Vector{Int64} <: Vector{<:Real}
true
julia> ## Mais attention!
julia> Int64 <: Integer, Vector{Int64} <: Vector{Integer}
(true, false)