Created
September 20, 2021 04:07
-
-
Save jakewilliami/26ca39bf79f93e66580317e4477b9c31 to your computer and use it in GitHub Desktop.
Rust Options in Julia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Some{T} | |
val::T | |
function Some{T}(val::T) where {T} | |
isnothing(val) && error("Cannot have some of nothing") | |
return new{T}(val) | |
end | |
end | |
Some(val::T) where {T} = Some{T}(val) | |
struct Option{T} | |
val::Union{Some{T}, Nothing} | |
end | |
Option(arg::T) where {T} = Option{T}(val) | |
unwrap(some::Some{T}) where {T} = some.val | |
is_none(opt::Option{T}) where {T} = isnothing(opt.val) | |
is_some(opt::Option{T}) where {T} = !is_none(opt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment