Skip to content

Instantly share code, notes, and snippets.

@andyferris
Created October 21, 2016 01:44
Show Gist options
  • Save andyferris/b1bd9d4f251e00bc3639d4ae47868e41 to your computer and use it in GitHub Desktop.
Save andyferris/b1bd9d4f251e00bc3639d4ae47868e41 to your computer and use it in GitHub Desktop.
Using a `?` as a nullable typename
module Maybe
export ?
immutable ?{T}
hasvalue::Bool
value::T
?() = new(false)
?(x::T) = new(true, x)
end
?{T}(x::T) = ?{T}(x)
?{T}(::Type{T}) = ?{T}()
Base.:*(::Type{?}, x) = ?(x) # I'm confused... ? is a unary in the Scheme file
function Base.show{T}(io::IO, x::?{T})
if x.hasvalue
print(io, x.value)
else
print(io, "?(", T, ")")
end
end
end # module
@nalimilan
Copy link

Interesting. Though it would still be good to have a real name for that type, with ? only an alias. Else, searches are going to be hard, and for dispatch x::?{T} looks really weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment