Created
October 21, 2016 01:44
-
-
Save andyferris/b1bd9d4f251e00bc3639d4ae47868e41 to your computer and use it in GitHub Desktop.
Using a `?` as a nullable typename
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 dispatchx::?{T}
looks really weird.