Skip to content

Instantly share code, notes, and snippets.

@jakewilliami
Created September 20, 2021 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakewilliami/26ca39bf79f93e66580317e4477b9c31 to your computer and use it in GitHub Desktop.
Save jakewilliami/26ca39bf79f93e66580317e4477b9c31 to your computer and use it in GitHub Desktop.
Rust Options in Julia
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