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?
>
> Cheers
>

Reply via email to