这是为了简化我在这里提出的问题的一部分:
我想写一些代码,保证在符合特定条件的类型上工作.我们今天要写一些代码:
immutable Example
whatever::ASCIIString
end
function step_one(x::Example)
length(x.whatever)
end
function step_two(x::Int64)
(x * 2.5)::Float64
end
function combine_two_steps{X}(x::X)
middle = step_one(x)
result = step_two(middle)
result
end
x = Example("Hi!")
combine_two_steps(x)
Run Code Online (Sandbox Code Playgroud)
运行此工作:
julia> x = Example("Hi!")
Example("Hi!")
julia> combine_two_steps(x)
7.5
Run Code Online (Sandbox Code Playgroud)
然后另一天我写了更多代码:
immutable TotallyDifferentExample
whatever::Bool
end
function step_one(x::TotallyDifferentExample)
if x.whatever
"Hurray"
else
"Boo"
end
end
function step_two(x::ASCIIString)
(Int64(Char(x[end])) * 1.5)::Float64
end
Run Code Online (Sandbox Code Playgroud)
你知道什么,我的通用组合功能仍然有效!
julia> y = TotallyDifferentExample(false)
TotallyDifferentExample(false)
julia> combine_two_steps(y)
166.5
Run Code Online (Sandbox Code Playgroud)
欢呼!但是,说这是一个深夜,我试图在第三个例子再次这样做.我记得要实施step_one,但我忘了实施step_two!
immutable ForgetfulExample
whatever::Float64
end
function …Run Code Online (Sandbox Code Playgroud)