Skip to content

Instantly share code, notes, and snippets.

@andyferris
Created October 21, 2016 01:44
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 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
@andyferris
Copy link
Author

andyferris commented Oct 21, 2016

Kind of amusing. E.g.

julia> using Maybe

julia> x = 3
3

julia>  ?x
3

julia> typeof(?x)
Maybe.?{Int64}

julia>  ?Int
?(Int64)

@andyferris
Copy link
Author

On this note we have ?, !, ¿ and ¡ (spanish style inverted question/exclamation marks). And unicode defines a reverse question mark which wikipedia describes as the irony mark. Between these you could develop a whole language for get, isnull/hasvalue, construction, type names, module names, and so-on.

@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