Julia : Some, something, Nothing

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wowotuo/article/details/81842257

突然一看,Julia什么时加上Some? 不会Julia也学上了Rust?
Some,是经常在Rust中常看见的用法。在Rust中,用Some、None搭配。

Rust: 对值进行封装

let x = Some("air");
assert_eq!(x.unwrap(), "air");

let x: Option<&str> = None;
assert_eq!(x.unwrap(), "air"); // fails

但是Julia呢?其实也差不多。

julia> a =Some(25) # 包了一个25值
Some(25)

julia> b =something(a) # 解封a, 取出a中的值
25

julia> b =something(a,nothing) # 解封第一个some中的值
25

julia> b =something(nothing)
ERROR: ArgumentError: No value arguments present
Stacktrace:
 [1] something() at .\some.jl:67
 [2] something(::Nothing) at .\some.jl:68
 [3] top-level scope at none:0

julia> c =Some("some value")
Some("some value")

julia> d =something(c,a) # 解封第一个some中的值
"some value"

julia> e =something(a,c) # 解封第一个some中的值
25

有趣的是,

julia> f =Some(Nothing)
Some(Nothing)

julia> g =something(f)
Nothing

julia> h =something(g,missing)
Nothing

julia> i =something(missing,g)
missing

猜你喜欢

转载自blog.csdn.net/wowotuo/article/details/81842257