From 8da3c10afadc9e93ab20822a0a76be74b98fa491 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Wed, 14 Sep 2016 21:49:26 +0200 Subject: [PATCH] Change isnull field of Nullable into hasvalue This is consistent with most other languages and formats. --- base/base.jl | 6 +++--- base/nullable.jl | 14 +++++++------- src/builtins.c | 24 ++++++++++++------------ src/julia.h | 4 ++-- test/core.jl | 4 ++-- test/nullable.jl | 14 +++++++------- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/base/base.jl b/base/base.jl index 65ea7d517bb8d..09992e6b0834f 100644 --- a/base/base.jl +++ b/base/base.jl @@ -146,9 +146,9 @@ gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Cint,), full) gc_enable(on::Bool) = ccall(:jl_gc_enable, Cint, (Cint,), on)!=0 immutable Nullable{T} - isnull::Bool + hasvalue::Bool value::T - Nullable() = new(true) - Nullable(value::T, isnull::Bool=false) = new(isnull, value) + Nullable() = new(false) + Nullable(value::T, isnull::Bool=false) = new(!isnull, value) end diff --git a/base/nullable.jl b/base/nullable.jl index df1228b60e65e..649dabff41641 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -53,20 +53,20 @@ otherwise, returns `y` if provided, or throws a `NullException` if not. """ @inline function get{S,T}(x::Nullable{S}, y::T) if isbits(S) - ifelse(x.isnull, y, x.value) + ifelse(isnull(x), y, x.value) else - x.isnull ? y : x.value + isnull(x) ? y : x.value end end -get(x::Nullable) = x.isnull ? throw(NullException()) : x.value +get(x::Nullable) = isnull(x) ? throw(NullException()) : x.value -isnull(x::Nullable) = x.isnull +isnull(x::Nullable) = !x.hasvalue function isequal(x::Nullable, y::Nullable) - if x.isnull && y.isnull + if isnull(x) && isnull(y) return true - elseif x.isnull || y.isnull + elseif isnull(x) || isnull(y) return false else return isequal(x.value, y.value) @@ -78,7 +78,7 @@ end const nullablehash_seed = UInt === UInt64 ? 0x932e0143e51d0171 : 0xe51d0171 function hash(x::Nullable, h::UInt) - if x.isnull + if isnull(x) return h + nullablehash_seed else return hash(x.value, h + nullablehash_seed) diff --git a/src/builtins.c b/src/builtins.c index ab42d879286fc..c1ef3ef786663 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -822,7 +822,7 @@ JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, si char *bstr = str+offset; char *pend = bstr+len; char *tofree = NULL; - int err = 0; + int hasvalue = 0; errno = 0; if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) { @@ -842,28 +842,28 @@ JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, si double out = jl_strtod_c(bstr, &p); if (errno==ERANGE && (out==0 || out==HUGE_VAL || out==-HUGE_VAL)) { - err = 1; + hasvalue = 0; } else if (p == bstr) { - err = 1; + hasvalue = 0; } else { // Deal with case where the substring might be something like "1 ", // which is OK, and "1 X", which we don't allow. - err = substr_isspace(p, pend) ? 0 : 1; + hasvalue = substr_isspace(p, pend) ? 1 : 0; } if (__unlikely(tofree)) free(tofree); - jl_nullable_float64_t ret = {(uint8_t)err, out}; + jl_nullable_float64_t ret = {(uint8_t)hasvalue, out}; return ret; } JL_DLLEXPORT int jl_substrtod(char *str, size_t offset, size_t len, double *out) { jl_nullable_float64_t nd = jl_try_substrtod(str, offset, len); - if (0 == nd.isnull) { + if (0 != nd.hasvalue) { *out = nd.value; return 0; } @@ -881,7 +881,7 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si char *bstr = str+offset; char *pend = bstr+len; char *tofree = NULL; - int err = 0; + int hasvalue = 0; errno = 0; if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) { @@ -905,28 +905,28 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si #endif if (errno==ERANGE && (out==0 || out==HUGE_VALF || out==-HUGE_VALF)) { - err = 1; + hasvalue = 0; } else if (p == bstr) { - err = 1; + hasvalue = 0; } else { // Deal with case where the substring might be something like "1 ", // which is OK, and "1 X", which we don't allow. - err = substr_isspace(p, pend) ? 0 : 1; + hasvalue = substr_isspace(p, pend) ? 1 : 0; } if (__unlikely(tofree)) free(tofree); - jl_nullable_float32_t ret = {(uint8_t)err, out}; + jl_nullable_float32_t ret = {(uint8_t)hasvalue, out}; return ret; } JL_DLLEXPORT int jl_substrtof(char *str, int offset, size_t len, float *out) { jl_nullable_float32_t nf = jl_try_substrtof(str, offset, len); - if (0 == nf.isnull) { + if (0 != nf.hasvalue) { *out = nf.value; return 0; } diff --git a/src/julia.h b/src/julia.h index 0c72dd0dd0703..4b88645b0bf10 100644 --- a/src/julia.h +++ b/src/julia.h @@ -1737,12 +1737,12 @@ JL_DLLEXPORT const char *jl_git_commit(void); // nullable struct representations typedef struct { - uint8_t isnull; + uint8_t hasvalue; double value; } jl_nullable_float64_t; typedef struct { - uint8_t isnull; + uint8_t hasvalue; float value; } jl_nullable_float32_t; diff --git a/test/core.jl b/test/core.jl index e920d418106fd..5a8c017bf0cd8 100644 --- a/test/core.jl +++ b/test/core.jl @@ -3118,13 +3118,13 @@ f11858(Any[Type{Foo11858}, Type{Bar11858}, typeof(g11858)]) foo11904(x::Int) = x @inline function foo11904{S}(x::Nullable{S}) if isbits(S) - Nullable(foo11904(x.value), x.isnull) + Nullable(foo11904(x.value), isnull(x)) else throw_error() end end -@test !foo11904(Nullable(1)).isnull +@test !isnull(foo11904(Nullable(1))) # issue 11874 immutable Foo11874 diff --git a/test/nullable.jl b/test/nullable.jl index 7ef888f2d87ed..d986be382680d 100644 --- a/test/nullable.jl +++ b/test/nullable.jl @@ -19,7 +19,7 @@ types = [ # Nullable{T}() = new(true) for T in types x = Nullable{T}() - @test x.isnull === true + @test x.hasvalue === false @test isa(x.value, T) @test eltype(Nullable{T}) === T @test eltype(x) === T @@ -28,13 +28,13 @@ end # Nullable{T}(value::T) = new(false, value) for T in types x = Nullable{T}(zero(T)) - @test x.isnull === false + @test x.hasvalue === true @test isa(x.value, T) @test x.value === zero(T) @test eltype(x) === T x = Nullable{T}(one(T)) - @test x.isnull === false + @test x.hasvalue === true @test isa(x.value, T) @test x.value === one(T) @test eltype(x) === T @@ -43,13 +43,13 @@ end # Nullable{T}(value::T, isnull::Bool) = new(isnull, value) for T in types x = Nullable{T}(zero(T),false) - @test x.isnull === false + @test x.hasvalue === true @test isa(x.value, T) @test x.value === zero(T) @test eltype(x) === T x = Nullable{T}(zero(T),true) - @test x.isnull === true + @test x.hasvalue === false @test isa(x.value, T) @test eltype(Nullable{T}) === T @test eltype(x) === T @@ -64,13 +64,13 @@ end for T in types v = zero(T) x = Nullable(v) - @test x.isnull === false + @test x.hasvalue === true @test isa(x.value, T) @test x.value === v v = one(T) x = Nullable(v) - @test x.isnull === false + @test x.hasvalue === true @test isa(x.value, T) @test x.value === v end