What's wrong with parameters? Are you making life harder for yourself than you 
need to? :-)

If you're worried about the (very slight) overhead (isleaftype calls out to C, 
and therefore can't be inlined), then you could use a Union where you specify 
all the types you allow. Or define the following function:

fastleaftype{T}(::Type{T}) = error("Must be a concrete type")

for x in names(Core)
        T = eval(x)
        if isleaftype(T) && T <: Number
            @eval begin
                fastleaftype(::Type{$T}) = nothing
            end
        end
end

Then:

function myfunction(x::Array)
    fastleaftype(eltype(x))
    x .+ 1
end

Now check this out:
julia> code_typed(myfunction, (Matrix{Float64},))
1-element Array{Any,1}:
 :($(Expr(:lambda, {:x}, {{},{{:x,Array{Float64,2},0}},{}}, :(begin  # none, 
line 2:
        nothing # line 3:
        return x::Array{Float64,2} .+ 1::Array{Float64,2}
    end::Array{Float64,2}))))

julia> code_typed(myfunction, (Matrix{Real},))
1-element Array{Any,1}:
 :($(Expr(:lambda, {:x}, {{},{{:x,Array{Real,2},0}},{}}, :(begin  # none, line 
2:
        throw($(Expr(:new, :(top(getfield)
(Base,:ErrorException)::Type{ErrorException}), "Must be a concrete 
type"))::ErrorException)::None # line 3:
        return x::Array{Real,2} .+ 1::Array{Real,2}
    end::Array{Real,2}))))


The compiler does the check for you, and this adds nothing whatsoever to your 
runtime.

--Tim


On Wednesday, April 30, 2014 07:37:05 AM Oliver Woodford wrote:
> On Wednesday, April 30, 2014 3:20:23 PM UTC+1, Tim Holy wrote:
> > You can add this as the first line of your function:
> >    assert_leaftype(T)
> > 
> > where
> > 
> >     assert_leaftype(T) = isleaftype(T) || error("Must be a concrete type")
> > 
> > To your users, this is at least as useful as
> > 
> >     ERROR: no method myfunction(Array{Real,1})
> > 
> > which is what it would be if you relied on method dispatch to generate the
> > error (which is what I think you're asking for).
> > 
> > --Tim
> 
> It's OK, but it still requires me to have a static parameter. Is there a
> O(1) time solution which avoids the need for static parameters. Something
> like:
> 
> function frob(x::Array)
> isleaftype(x) || error("Homogeneous array required")
> 
> though I know this won't work.

Reply via email to