Le vendredi 10 juin 2016 à 00:56 -0700, Helge Eichhorn a écrit :
> Hi,
>
> let's say I have the following type with two Nullable fields:
>
> type WithNulls
> a::Nullable{Float64}
> b::Nullable{Float64}
> end
>
> I now want the user to be able to create an instance of this type
> without caring about Nullables. For this I use a constructor with
> keyword arguments.
>
> function WithNulls(;
> a = Nullable{Float64}(),
> b = Nullable{Float64}(),
> )
> WithNulls(a, b)
> end
>
> This works for Float64 but not for the other leaf types of Real.
>
> # Works
> WithNulls(a=3.0)
>
> # Does not work
> WithNulls(a=pi)
>
> This can be fixed by adding the following methods to convert:
>
> Base.convert{T<:Real}(::Type{Nullable{T}}, v::T) = Nullable{T}(v)
> Base.convert{T<:Real,S<:Real}(::Type{Nullable{T}}, v::S) =
> Nullable{T}(convert(T,v))
>
> Finally the question:
> Should the above convert methods not be part of Base? I think
> converting between different Nullable{T<:Real} values might be a
> common use case. Is there a more elegant way to do this?
Yes. These methods have been added in the 0.5 development version, so
your example works directly there.
Regards