If your initial knn function has logic that depends on the concrete type of
tree being input, it's a sign you should refactor that logic into separate
methods which are dispatched on by the concrete type. The simplest case of
this would be to just pass that argument during setup to every _knn
function which then may or may not choose to ignore it.
In general, it's always possible to design the abstract interface methods
to not have any knowledge of concrete implementation.
On Saturday, February 21, 2015 at 12:41:42 PM UTC-5, Kristoffer Carlsson
wrote:
>
> Hello,
>
> I am getting some coupling between my types and I don't really know how to
> solve it in a good way.
>
> As shown below in the code I have a module with 3 files, one with an
> abstract type and two that are concrete types of the abstract one.
>
> In the file with the abstract type I have a function that does some setup
> stuff and then depending on
> what concrete type is passed it calls the correct function. These have
> different arguments so I can't just dispatch on the concrete type.
>
> The problem I have is that in the abstract type the concrete type is not
> defined yet so I get an error.
> And I can't define the abstract type last because the concrete types need
> to know about it.
>
> What I end up doing is basically define all my types first and then the
> functions. This however, leads to my code being structured in a way which I
> don't like.
>
> I want to structure my program something like this:
>
> # NearestNeighbour.jl
> #####################
> module NearestNeighbour
> include("nntree.jl")
> include("balltree.jl")
> include("kdtree.jl")
> end
>
> # nntree.jl
> ####################
> abstract NNTree
>
> function knn(tree::NTree)
>
> # Do a bunch of setup stuff
>
> if tree <: BallTree
> _knn(tree, arg)
> else
> _knn(tree)
>
> # kdtree.jl
> ######################
> immutable KDTree <: NNTree
> param::Type
> end
>
> function _knn(tree::KDTree)
> bleh()
> end
>
> # balltree.jl
> ######################
> immutable BallTree <: NNTree
> param::Type
> param2::Type
> end
>
> function _knn(tree::BallTree, arg)
> bleh2()
> end
>
> As I said, this gives an error becuase knn does not know about the
> BallTree type. Maybe there is a cleaner solution to this than to have a
> "tree_types.jl" file that just defines all the types before the functions
> come.
>
> Best regards,
> Kristoffer Carlsson
>