-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from shashi/nullable
Add Nullable{T}
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,4 +173,8 @@ end | |
|
||
export @compat, @inline, @noinline | ||
|
||
if VERSION < v"0.4.0-dev+656" | ||
include("nullable.jl") | ||
end | ||
|
||
end # module |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Base: eltype, convert, get, isequal, ==, hash, show | ||
export Nullable, NullException, isnull | ||
|
||
immutable Nullable{T} | ||
isnull::Bool | ||
value::T | ||
|
||
Nullable() = new(true) | ||
Nullable(value::T) = new(false, value) | ||
end | ||
|
||
immutable NullException <: Exception | ||
end | ||
|
||
Nullable{T}(value::T) = Nullable{T}(value) | ||
Nullable() = Nullable{Union()}() | ||
|
||
eltype{T}(::Type{Nullable{T}}) = T | ||
|
||
function convert{T}(::Type{Nullable{T}}, x::Nullable) | ||
return isnull(x) ? Nullable{T}() : Nullable{T}(convert(T, get(x))) | ||
end | ||
|
||
convert{T}(::Type{Nullable{T}}, x::T) = Nullable{T}(x) | ||
|
||
convert{T}(::Type{Nullable{T}}, ::Void) = Nullable{T}() | ||
convert( ::Type{Nullable }, ::Void) = Nullable{Union()}() | ||
|
||
function show{T}(io::IO, x::Nullable{T}) | ||
if x.isnull | ||
@printf(io, "Nullable{%s}()", repr(T)) | ||
else | ||
@printf(io, "Nullable(%s)", repr(x.value)) | ||
end | ||
end | ||
|
||
get(x::Nullable) = x.isnull ? throw(NullException()) : x.value | ||
|
||
get{T}(x::Nullable{T}, y) = x.isnull ? convert(T, y) : x.value | ||
|
||
isnull(x::Nullable) = x.isnull | ||
|
||
function isequal(x::Nullable, y::Nullable) | ||
if x.isnull && y.isnull | ||
return true | ||
elseif x.isnull || y.isnull | ||
return false | ||
else | ||
return isequal(x.value, y.value) | ||
end | ||
end | ||
|
||
==(x::Nullable, y::Nullable) = throw(NullException()) | ||
|
||
const nullablehash_seed = UInt === UInt64 ? 0x932e0143e51d0171 : 0xe51d0171 | ||
|
||
function hash(x::Nullable, h::UInt) | ||
if x.isnull | ||
return h + nullablehash_seed | ||
else | ||
return hash(x.value, h + nullablehash_seed) | ||
end | ||
end |