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