Skip to content

Commit f9e34c3

Browse files
committed
Change isnull field of Nullable into hasvalue
This is consistent with most other languages and formats.
1 parent 792ded8 commit f9e34c3

File tree

6 files changed

+38
-39
lines changed

6 files changed

+38
-39
lines changed

base/base.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Cint,), full)
146146
gc_enable(on::Bool) = ccall(:jl_gc_enable, Cint, (Cint,), on)!=0
147147

148148
immutable Nullable{T}
149-
isnull::Bool
149+
hasvalue::Bool
150150
value::T
151151

152-
Nullable() = new(true)
153-
Nullable(value::T, isnull::Bool=false) = new(isnull, value)
152+
Nullable() = new(false)
153+
Nullable(value::T, isnull::Bool=false) = new(!isnull, value)
154154
end

base/nullable.jl

+12-13
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ otherwise, returns `y` if provided, or throws a `NullException` if not.
5353
"""
5454
@inline function get{S,T}(x::Nullable{S}, y::T)
5555
if isbits(S)
56-
ifelse(x.isnull, y, x.value)
56+
ifelse(isnull(x), y, x.value)
5757
else
58-
x.isnull ? y : x.value
58+
isnull(x) ? y : x.value
5959
end
6060
end
6161

62-
get(x::Nullable) = x.isnull ? throw(NullException()) : x.value
63-
64-
isnull(x::Nullable) = x.isnull
62+
get(x::Nullable) = isnull(x) ? throw(NullException()) : x.value
6563

64+
isnull(x::Nullable) = !x.hasvalue
6665

6766
## Operators
6867

@@ -107,15 +106,15 @@ and `false` if one is null but not the other: nulls are considered equal.
107106
"""
108107
@inline function isequal{S,T}(x::Nullable{S}, y::Nullable{T})
109108
if null_safe_op(isequal, S, T)
110-
(x.isnull & y.isnull) | (!x.isnull & !y.isnull & isequal(x.value, y.value))
109+
(isnull(x) & isnull(y)) | (!isnull(x) & !isnull(y) & isequal(x.value, y.value))
111110
else
112-
(x.isnull & y.isnull) || (!x.isnull & !y.isnull && isequal(x.value, y.value))
111+
(isnull(x) & isnull(y)) || (!isnull(x) & !isnull(y) && isequal(x.value, y.value))
113112
end
114113
end
115114

116115
isequal(x::Nullable{Union{}}, y::Nullable{Union{}}) = true
117-
isequal(x::Nullable{Union{}}, y::Nullable) = y.isnull
118-
isequal(x::Nullable, y::Nullable{Union{}}) = x.isnull
116+
isequal(x::Nullable{Union{}}, y::Nullable) = isnull(y)
117+
isequal(x::Nullable, y::Nullable{Union{}}) = isnull(x)
119118

120119
null_safe_op{S<:NullSafeTypes,
121120
T<:NullSafeTypes}(::typeof(isless), ::Type{S}, ::Type{T}) = true
@@ -135,22 +134,22 @@ another null.
135134
@inline function isless{S,T}(x::Nullable{S}, y::Nullable{T})
136135
# NULL values are sorted last
137136
if null_safe_op(isless, S, T)
138-
(!x.isnull & y.isnull) | (!x.isnull & !y.isnull & isless(x.value, y.value))
137+
(!isnull(x) & isnull(y)) | (!isnull(x) & !isnull(y) & isless(x.value, y.value))
139138
else
140-
(!x.isnull & y.isnull) || (!x.isnull & !y.isnull && isless(x.value, y.value))
139+
(!isnull(x) & isnull(y)) || (!isnull(x) & !isnull(y) && isless(x.value, y.value))
141140
end
142141
end
143142

144143
isless(x::Nullable{Union{}}, y::Nullable{Union{}}) = false
145144
isless(x::Nullable{Union{}}, y::Nullable) = false
146-
isless(x::Nullable, y::Nullable{Union{}}) = !x.isnull
145+
isless(x::Nullable, y::Nullable{Union{}}) = !isnull(x)
147146

148147
==(x::Nullable, y::Nullable) = throw(NullException())
149148

150149
const nullablehash_seed = UInt === UInt64 ? 0x932e0143e51d0171 : 0xe51d0171
151150

152151
function hash(x::Nullable, h::UInt)
153-
if x.isnull
152+
if isnull(x)
154153
return h + nullablehash_seed
155154
else
156155
return hash(x.value, h + nullablehash_seed)

src/builtins.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, si
807807
char *bstr = str+offset;
808808
char *pend = bstr+len;
809809
char *tofree = NULL;
810-
int err = 0;
810+
int hasvalue = 0;
811811

812812
errno = 0;
813813
if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) {
@@ -827,28 +827,28 @@ JL_DLLEXPORT jl_nullable_float64_t jl_try_substrtod(char *str, size_t offset, si
827827
double out = jl_strtod_c(bstr, &p);
828828

829829
if (errno==ERANGE && (out==0 || out==HUGE_VAL || out==-HUGE_VAL)) {
830-
err = 1;
830+
hasvalue = 0;
831831
}
832832
else if (p == bstr) {
833-
err = 1;
833+
hasvalue = 0;
834834
}
835835
else {
836836
// Deal with case where the substring might be something like "1 ",
837837
// which is OK, and "1 X", which we don't allow.
838-
err = substr_isspace(p, pend) ? 0 : 1;
838+
hasvalue = substr_isspace(p, pend) ? 1 : 0;
839839
}
840840

841841
if (__unlikely(tofree))
842842
free(tofree);
843843

844-
jl_nullable_float64_t ret = {(uint8_t)err, out};
844+
jl_nullable_float64_t ret = {(uint8_t)hasvalue, out};
845845
return ret;
846846
}
847847

848848
JL_DLLEXPORT int jl_substrtod(char *str, size_t offset, size_t len, double *out)
849849
{
850850
jl_nullable_float64_t nd = jl_try_substrtod(str, offset, len);
851-
if (0 == nd.isnull) {
851+
if (0 != nd.hasvalue) {
852852
*out = nd.value;
853853
return 0;
854854
}
@@ -866,7 +866,7 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si
866866
char *bstr = str+offset;
867867
char *pend = bstr+len;
868868
char *tofree = NULL;
869-
int err = 0;
869+
int hasvalue = 0;
870870

871871
errno = 0;
872872
if (!(*pend == '\0' || isspace((unsigned char)*pend) || *pend == ',')) {
@@ -890,28 +890,28 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si
890890
#endif
891891

892892
if (errno==ERANGE && (out==0 || out==HUGE_VALF || out==-HUGE_VALF)) {
893-
err = 1;
893+
hasvalue = 0;
894894
}
895895
else if (p == bstr) {
896-
err = 1;
896+
hasvalue = 0;
897897
}
898898
else {
899899
// Deal with case where the substring might be something like "1 ",
900900
// which is OK, and "1 X", which we don't allow.
901-
err = substr_isspace(p, pend) ? 0 : 1;
901+
hasvalue = substr_isspace(p, pend) ? 1 : 0;
902902
}
903903

904904
if (__unlikely(tofree))
905905
free(tofree);
906906

907-
jl_nullable_float32_t ret = {(uint8_t)err, out};
907+
jl_nullable_float32_t ret = {(uint8_t)hasvalue, out};
908908
return ret;
909909
}
910910

911911
JL_DLLEXPORT int jl_substrtof(char *str, int offset, size_t len, float *out)
912912
{
913913
jl_nullable_float32_t nf = jl_try_substrtof(str, offset, len);
914-
if (0 == nf.isnull) {
914+
if (0 != nf.hasvalue) {
915915
*out = nf.value;
916916
return 0;
917917
}

src/julia.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1737,12 +1737,12 @@ JL_DLLEXPORT const char *jl_git_commit(void);
17371737

17381738
// nullable struct representations
17391739
typedef struct {
1740-
uint8_t isnull;
1740+
uint8_t hasvalue;
17411741
double value;
17421742
} jl_nullable_float64_t;
17431743

17441744
typedef struct {
1745-
uint8_t isnull;
1745+
uint8_t hasvalue;
17461746
float value;
17471747
} jl_nullable_float32_t;
17481748

test/core.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3126,13 +3126,13 @@ f11858(Any[Type{Foo11858}, Type{Bar11858}, typeof(g11858)])
31263126
foo11904(x::Int) = x
31273127
@inline function foo11904{S}(x::Nullable{S})
31283128
if isbits(S)
3129-
Nullable(foo11904(x.value), x.isnull)
3129+
Nullable(foo11904(x.value), isnull(x))
31303130
else
31313131
throw_error()
31323132
end
31333133
end
31343134

3135-
@test !foo11904(Nullable(1)).isnull
3135+
@test !isnull(foo11904(Nullable(1)))
31363136

31373137
# issue 11874
31383138
immutable Foo11874

test/nullable.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ types = [
1919
# Nullable{T}() = new(true)
2020
for T in types
2121
x = Nullable{T}()
22-
@test x.isnull === true
22+
@test x.hasvalue === false
2323
@test isa(x.value, T)
2424
@test eltype(Nullable{T}) === T
2525
@test eltype(x) === T
@@ -28,13 +28,13 @@ end
2828
# Nullable{T}(value::T) = new(false, value)
2929
for T in types
3030
x = Nullable{T}(zero(T))
31-
@test x.isnull === false
31+
@test x.hasvalue === true
3232
@test isa(x.value, T)
3333
@test x.value === zero(T)
3434
@test eltype(x) === T
3535

3636
x = Nullable{T}(one(T))
37-
@test x.isnull === false
37+
@test x.hasvalue === true
3838
@test isa(x.value, T)
3939
@test x.value === one(T)
4040
@test eltype(x) === T
@@ -43,13 +43,13 @@ end
4343
# Nullable{T}(value::T, isnull::Bool) = new(isnull, value)
4444
for T in types
4545
x = Nullable{T}(zero(T),false)
46-
@test x.isnull === false
46+
@test x.hasvalue === true
4747
@test isa(x.value, T)
4848
@test x.value === zero(T)
4949
@test eltype(x) === T
5050

5151
x = Nullable{T}(zero(T),true)
52-
@test x.isnull === true
52+
@test x.hasvalue === false
5353
@test isa(x.value, T)
5454
@test eltype(Nullable{T}) === T
5555
@test eltype(x) === T
@@ -64,13 +64,13 @@ end
6464
for T in types
6565
v = zero(T)
6666
x = Nullable(v)
67-
@test x.isnull === false
67+
@test x.hasvalue === true
6868
@test isa(x.value, T)
6969
@test x.value === v
7070

7171
v = one(T)
7272
x = Nullable(v)
73-
@test x.isnull === false
73+
@test x.hasvalue === true
7474
@test isa(x.value, T)
7575
@test x.value === v
7676
end

0 commit comments

Comments
 (0)