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
On Wednesday, April 30, 2014 06:02:37 AM Oliver Woodford wrote:
> On Tuesday, April 29, 2014 7:43:03 PM UTC+1, Tomas Lycken wrote:
> > OK, well, just because I wrote that, I realize there is one problem that I
> > can't off the top of my head say how I'd solve: type assertions for e.g.
> > arrays. Say I have a variable x, and I want to make sure that it's an
> > array
> > of real numbers that are all of the same type, but I don't care which one.
> > Can I say x::Array{T<:Real} as a type assertion? (I'm at a computer
> > without
> > Julia atm, so I can't test it for myself...)
>
> No, you can't do x::Array{T<:Real} or function f{T<:Real}(x::Array{T}) and
> have that enforce homogeneous arrays.
>
> This is exactly the scenario I'm in. Yes (to address an earlier point), if
> I didn't bother with the type assertion, Julia would do the optimal thing
> in each case. However, my function will be a lot slower if you pass in a
> heterogeneous array, and I want to avoid programmers accidentally doing
> that. This is why I started this thread! Secondly, I don't think that
> should be done using a static parameter, either (not that it is).
>
> I agree with your other points on how nice Julia is, but I want to tackle
> the one narly point!