include(joinpath(@__DIR__, "observations.jl"))
reading(x::Float64,y::Float64)=2x+y
reading(x::Number,y::Number)=2x-y
probe("method_dispatch",(7,11),(reading(7.0,11.0),reading(7,11.0),reading(7,11),
outcome(() -> reading("bay","hill")),length(methods(reading))),
(25.0,3.0,3,"MethodError",2))
route(x::Float64,y)=2x+y
route(x,y::Float64)=x+2y
diagnostic=try
route(7.0,11.0)
catch err
(err isa MethodError,occursin("ambiguous",sprint(showerror,err)))
end
probe("method_ambiguous",(7.0,11.0),(route(7.0,11),route(7,11.0),diagnostic),(25.0,29.0,(true,true)))
route(x::Float64,y::Float64)=2x+2y
probe("method_disambiguated",(7.0,11.0),(route(7.0,11.0),length(methods(route))),(36.0,3))
same(x::T,y::T) where {T}=true
same(x,y)=false
same_numeric(x::T,y::T) where {T<:Number}=true
appended(v::Vector{T},x::T) where {T}=[v...,x]
probe("method_where",(7,11,11.0),(same(7,11),same(7,11.0),same_numeric(7,11),
outcome(() -> same_numeric("N","S"))),(true,false,true,"MethodError"))
probe("method_tied_parameters",([7,11],13,13.5),(appended([7,11],13),outcome(() -> appended([7,11],13.5))),
([7,11,13],"MethodError"))
sum_count(x::Integer)=x+1
take_readings(v::Vector{<:Real})=sum(v)
probe("method_broad_integer",big(7),(sum_count(big(7)),typeof(sum_count(big(7))),outcome(() -> sum_count(7.0))),
(big(8),BigInt,"MethodError"))
probe("method_vector_signature",([7,11],Real[7,11.0]),(take_readings([7,11]),take_readings(Real[7,11.0]),
outcome(() -> take_readings(["N","S"]))),(18,18.0,"MethodError"))
abstract type Region end
struct RoundRegion <: Region
radius::Float64
end
struct RectRegion <: Region
width::Float64
height::Float64
end
area(region::RoundRegion)=pi*region.radius^2
area(region::RectRegion)=region.width*region.height
describe(region::Region)=("area",area(region))
probe("method_interface","two Region subtypes",(last(describe(RoundRegion(2)))≈4pi,describe(RectRegion(3,7))),
(true,("area",21.0)))
op=+
probe("method_operator_value",(7,11,13),(7+11+13,+(7,11,13),op(7,11)),(31,31,18))
struct Offset
x::Float64
y::Float64
end
Base.:+(a::Offset,b::Offset)=Offset(a.x+b.x,a.y+b.y)
shifted=Offset(7,11)+Offset(13,17)
probe("method_owned_extension","two owned Offset records",(shifted.x,shifted.y),(20.0,28.0))
module LocalOwner
struct Marker
name::Symbol
end
inspect(x::Marker)=(:initial,x.name)
end
owned=LocalOwner.Marker(:bay)
before=LocalOwner.inspect(owned)
module LocalIntruder
import ..LocalOwner
LocalOwner.inspect(x::LocalOwner.Marker)=(:modified,x.name)
end
probe("method_owned_piracy_model","two local teaching modules",(before,LocalOwner.inspect(owned)),
((:initial,:bay),(:modified,:bay)))
function blend(x::T,y::T) where {T<:Real}
x*y
end
blend(x::Real,y::Real)=blend(promote(x,y)...)
probe("method_promote",(7,1.5),(blend(7,1.5),typeof(blend(7,1.5)),blend(7,11)),(10.5,Float64,77))
struct Distance
meters::Float64
end
as_distance(x::Distance)=x
as_distance(x::Real)=Distance(Float64(x))
difference(x::Distance,y::Distance)=x.meters-y.meters
difference(x,y)=difference(as_distance(x),as_distance(y))
probe("method_normalize",(7,11),(difference(Distance(7),11),difference(7,Distance(11)),
difference(7,11),outcome(() -> difference("north",11))),(-4.0,-4.0,-4.0,"MethodError"))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-types-methods/methods.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
methods.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a215.zip