half(x::Int)::Int = x/2
half (generic function with 1 method)
half(2)
1
half(3)
InexactError: Int64(1.5) Stacktrace: [1] Int64 @ .\float.jl:812 [inlined] [2] convert @ .\number.jl:7 [inlined] [3] half(x::Int64) @ Main .\In[1]:1 [4] top-level scope @ In[3]:1 [5] eval @ .\boot.jl:373 [inlined] [6] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base .\loading.jl:1196
@code_lowered half(2)
CodeInfo( 1 ─ %1 = Main.Int │ %2 = x / 2 │ %3 = Base.convert(%1, %2) │ %4 = Core.typeassert(%3, %1) └── return %4 )
function f(n::Integer)::Vector{Real}
randn(n)
end
f (generic function with 1 method)
@time sum(randn(10^8))
0.352352 seconds (2 allocations: 762.939 MiB, 1.84% gc time)
-14842.156808955893
@time sum(f(10^8))
9.420157 seconds (200.00 M allocations: 4.470 GiB, 58.79% gc time, 0.03% compilation time)
4626.280496458812
abstract type AbstractFoo{T} end
struct Foo{T} <: AbstractFoo{T} a::T end
double(x::Foo) = Foo(2x.a)
struct Bar{T} <: AbstractFoo{T} a::T end
double(Foo(1.23))
Foo{Float64}(2.46)
double(Bar(1.23))
MethodError: no method matching double(::Bar{Float64})
Closest candidates are:
double(::Foo) at In[8]:4
Stacktrace:
[1] top-level scope
@ In[10]:1
[2] eval
@ .\boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
double(x::AbstractFoo) = throw(MethodError(double, (x,)))
double (generic function with 2 methods)
double(Bar(1.23))
MethodError: no method matching double(::Bar{Float64})
Closest candidates are:
double(::AbstractFoo) at In[11]:1
double(::Foo) at In[8]:4
Stacktrace:
[1] double(x::Bar{Float64})
@ Main .\In[11]:1
[2] top-level scope
@ In[12]:1
[3] eval
@ .\boot.jl:373 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
multiply(x::Foo, y::Number) = Foo(x.a * y)
multiply (generic function with 1 method)
multiply(Foo(3), 10.0)
Foo{Float64}(30.0)
multiply(Bar(3), 10.0)
MethodError: no method matching multiply(::Bar{Int64}, ::Float64)
Closest candidates are:
multiply(::Foo, ::Number) at In[13]:1
Stacktrace:
[1] top-level scope
@ In[15]:1
[2] eval
@ .\boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
multiply(x::AbstractFoo, y) = error("`myltiply` has not been implemented for $(typeof(x))")
multiply (generic function with 2 methods)
multiply(Bar(3), 10.0)
`myltiply` has not been implemented for Bar{Int64} Stacktrace: [1] error(s::String) @ Base .\error.jl:33 [2] multiply(x::Bar{Int64}, y::Float64) @ Main .\In[16]:1 [3] top-level scope @ In[17]:1 [4] eval @ .\boot.jl:373 [inlined] [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base .\loading.jl:1196
multiply(Foo(3), [10.0])
`myltiply` has not been implemented for Foo{Int64} Stacktrace: [1] error(s::String) @ Base .\error.jl:33 [2] multiply(x::Foo{Int64}, y::Vector{Float64}) @ Main .\In[16]:1 [3] top-level scope @ In[18]:1 [4] eval @ .\boot.jl:373 [inlined] [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base .\loading.jl:1196
multiply(x::AbstractFoo, y) = throw(MethodError(multiply, (x, y)))
multiply (generic function with 2 methods)
multiply(Foo(3), [10.0])
MethodError: no method matching multiply(::Foo{Int64}, ::Vector{Float64})
Closest candidates are:
multiply(::AbstractFoo, ::Any) at In[19]:1
multiply(::Foo, ::Number) at In[13]:1
Stacktrace:
[1] multiply(x::Foo{Int64}, y::Vector{Float64})
@ Main .\In[19]:1
[2] top-level scope
@ In[20]:1
[3] eval
@ .\boot.jl:373 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
function double!(a::Array)
@. a = 2a
end
double! (generic function with 1 method)
v = [1, 2, 3, 4]
@show double!(v)
@show v;
double!(v) = [2, 4, 6, 8] v = [2, 4, 6, 8]
w = view(v, 2:3)
2-element view(::Vector{Int64}, 2:3) with eltype Int64: 4 6
double!(w)
MethodError: no method matching double!(::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true})
Closest candidates are:
double!(::Array) at In[21]:1
Stacktrace:
[1] top-level scope
@ In[24]:1
[2] eval
@ .\boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
function double!(v)
@. v = 2v
end
double! (generic function with 2 methods)
@show double!(w)
@show w
@show v;
double!(w) = [8, 12] w = [8, 12] v = [2, 8, 12, 8]