Hello Julia Users,

I have wanted to use dispatch to enable the extraction of features or 
metrics. This way you can define a large collection of features that all 
extend AbstractFeature, and then just run through a list of them to get 
your feature vector.

One way this could be implemented is as follows:

abstract AbstractFeature
extract(f::AbstractFeature, ::State) = error("extract not implemented for 
AbstractFeature $f)

type FeatureA <: AbstractFeature end
extract(f::FeatureA, s::State) = 1.0

type FeatureB <: AbstractFeature end
extract(f::FeatureB, s::State) = 2.0

...


This is great in theory, but in practice you end up having to iterate over 
an abstract array, ::Vector{AbstractFeature}, which is slow.

Other options include only having one Feature type:
type Feature
   id::Int
end

function extract(f::Feature, s::State)
    if f.id == 1
       # code for feature 1...
    elseif f.id == 2
       # code for feature 2...
    elseif ...
    end
end

You lose the ability to define Features with different internal fields / 
parameters.

I was thinking you can also dispatch on the internal id value. Would this 
work?

type Feature
    id::Int
end
extract(::Type{Val{1}}, s::State) = 1.0
extract(::Type{Val{2}}, s::State) = 2.0

This is a little annoying because the ids are non-intuitive and you have to 
ensure there is no collision when you define new ones.

Any ideas / recommendations?


Reply via email to