Thanks

On Tuesday, March 31, 2015 at 8:49:22 PM UTC+13, Milan Bouchet-Valat wrote:
>
> Le lundi 30 mars 2015 à 18:03 -0700, Steve Cordwell a écrit : 
> > Actually there were some bugs in the code. The following code works: 
> > 
> > zero_mask!{T,N}(a::AbstractArray{T,N}, mask::AbstractArray{Bool,N}) = 
> setindex!(a, zero(T), find(!mask)) 
> > 
> > function do_stuff{N}(rows, columns, mask::Nullable{Array{Bool,N}}) 
> >     a = rand(rows, columns) 
> >     isnull(mask) ? nothing : zero_mask!(a, get(mask)) 
> >     a 
> > end 
> > 
> > do_stuff(rows, columns, mask::Array{Bool}) = do_stuff(rows, columns, 
> Nullable(mask)) 
> > 
> > do_stuff(rows, columns) = do_stuff(rows, columns, 
> Nullable{Array{Bool}}()) 
> > 
> > However, replacing Array with AbstractArray does not. 
> > 
> > On Tuesday, March 31, 2015 at 1:23:44 PM UTC+13, Steve Cordwell wrote: 
> >         Say I want to be able to set the value of an array to zero where 
> a boolean mask array is false, then I can define this function: 
> >         
> >             zero_mask!(a::AbstractArray, mask::AbstractArray{Bool}) = 
> setindex!(a, zero(T), find(!mask)) 
> >         
> >         I have a function that constructs arrays and part of its job is 
> to call zero_mask! if a mask is passed to it. I thought I could use 
> Nullable types so that there is only one implementation rather than two and 
> have helper methods call the do_stuff with the correct arguments. 
> >         
> >             function do_stuff(rows, columns, 
> mask::Nullable{AbstractArray{Bool}}) 
> >                 a = rand(rows, columns) 
> >                 isnull(mask) ? nothing : zero_mask!(a) 
> >                 a 
> >             end 
> >         
> >         It would be nice to use do_stuff with just a boolean array, 
> rather than specifying it to be Nullable. 
> >         
> >             do_stuff(rows, columns, mask::AbstractArray{Bool}) = 
> do_stuff(rows, columns, Nullable(mask)) 
> >         
> >             do_stuff(rows, columns) = do_stuff(rows, columns, 
> Nullable{AbstractArray{Bool}}()) 
> >         
> >         However, I get the following error: 
> >         
> >             do_stuff(10, 3, rand(Bool, 10, 3)) 
> >             ERROR: MethodError: `do_stuff` has no method matching 
> do_stuff(::Int64, ::Int64, ::Nullable{Array{Bool,2}}) 
> >         
> >         Presumably because I need to define do_study{N}(rows, columns, 
> Nullable{Array{Bool,N}}). Is there a way to use Nullable abstract types? 
> You're hitting the issue of invariance vs. covariance, about which you 
> should be able to find several threads in the archives (but still no 
> FAQ?!). 
>
> Just use this instead: 
> function do_stuff{T<:AbstractArray{Bool}}(rows, columns, 
> mask::Nullable{T}) 
>
>
> Regards 
>

Reply via email to