Why can you not make it into a function?

function foo!(spec::Symbol, A, i)
    A[i,spec] = 1
end
function foo!{T<:Real}(spec::Vector{(Symbol,T), A, i)
      @assert(isapprox(sum(map(x -> x[2],spec)),1))
      for (obs,prob) = spec
        A[i,obs] = prob
      end
    A[i,spec] = 1
end
foo!(spec, A, i)  = error(" ...")

that way your code will be cleaner too:

foo!(spec, A, i)

In Julia, when you use an isa, then ask yourself whether it might not be
cleaner using methods instead.

If that is not an option you have to make a function for the check...

On Mon, 2015-04-13 at 14:06, Tamas Papp <[email protected]> wrote:
> Hi,
>
> I had some code like
>
>     if isa(spec, Symbol)
>       A[i,spec] = 1
>     elseif isa(spec, Vector{(Symbol,Float64)})
>       @assert(isapprox(sum(map(x -> x[2],spec)),1))
>       for (obs,prob) = spec
>         A[i,obs] = prob
>       end
>     else
>       error("don't know how to parse $spec for aggregation")
>     end
>
> but got bittenby a spec = [(:I,0),(:U,1)] which is valid for my problem,
> so I realized I need to test for Real, not Float64.
>
> If this was a method of a generic fuction, I could simply use T <: Real
> and then use Vector{(Symbol,T)} in the type signature --- but it isn't,
> so I would be interested in how to test for the condition in the subject
> line in an if branch.
>
> isa(Vector{(Symbol,Real)}) won't work, my understanding is that this is
> because of invariance.
>
> Best,
>
> Tamas

Reply via email to