-
Notifications
You must be signed in to change notification settings - Fork 21
Can't construct a nullable vector from a vector of nullables #98
Comments
I think this will work if you define the appropriate |
OK, that works:
But we'll also need the outer constructor:
I will attempt a PR with both |
You don't need an outer constructor: you just need a different
|
OK, I had to use an outer constructor instead of |
Hmm. Define function Base.convert{T}(::Type{T}, x::Nullable{T})
if isnull(x)
error()
else
get(x)
end
end We get some ambiguity warnings. These aside, we then have the following: julia> A = [Nullable(1), Nullable(2)];
julia> B = [Nullable(1) Nullable(2); Nullable(1) Nullable(2)];
julia> NullableArray(A)
2-element NullableArrays.NullableArray{Nullable{Int64},1}:
Nullable{Int64}(1)
Nullable{Int64}(2)
julia> NullableArray(B)
2x2 NullableArrays.NullableArray{Nullable{Int64},2}:
Nullable{Int64}(1) Nullable{Int64}(2)
Nullable{Int64}(1) Nullable{Int64}(2)
julia> convert(NullableArray, A)
2-element NullableArrays.NullableArray{Nullable{Int64},1}:
Nullable{Int64}(1)
Nullable{Int64}(2)
julia> convert(NullableArray, B)
2x2 NullableArrays.NullableArray{Nullable{Int64},2}:
Nullable{Int64}(1) Nullable{Int64}(2)
Nullable{Int64}(1) Nullable{Int64}(2)
julia> NullableVector{Int}(A)
2-element NullableArrays.NullableArray{Int64,1}:
1
2
julia> NullableVector(A)
ERROR: MethodError: Cannot `convert` an object of type Array{Nullable{Int64},1} to an object of type NullableArrays.NullableArray{T,1}
This may have arisen from a call to the constructor NullableArrays.NullableArray{T,1}(...),
since type constructors fall back to convert methods.
Closest candidates are:
convert{T}(::Type{T}, ::Nullable{T})
convert{T}(::Type{T}, ::T)
(::Type{BoundsError})(::ANY)
...
in NullableArrays.NullableArray{T,1}(::Array{Nullable{Int64},1}) at ./int.jl:419
in eval(::Module, ::Any) at ./boot.jl:267 Not sure which I prefer: introducing conversion methods at the |
Sure, a convert method in Base might make sense, much like we can convert a But I think it is orthogonal/additional to this discussion. I can't see that your approach will work when some of the I suppose it would be more "complete" if we also defined the reverse transformation, but it's so easily done via |
@andyferris That's a good point. I hope to be able to take a closer look at your PR tomorrow and get it merged shortly. |
Is this resolved with #99 ? |
Case in point:
This seems really unintuitive.
I came across this when I tried to make Tables.jl understand about storing
Nullable
values inNullableArrays
, where I have some convenience constructors for when, e.g., a single row (or a small number of rows) of the table are used in instantiation and my constructor needs to convert one or moreNullable
s in a vector to aNullableVector
.Is there a strong reason this isn't implemented? Would you welcome a PR?
PS - Similar automatic conversion works with
Vector
:The text was updated successfully, but these errors were encountered: