I think in this case it does make sense to attach a function to an
object. (This is different from the OO discussion above, which is
about attaching a function to a type.)

immutable Sphere <: Manifold
    dim::Int
    metric::Function
end

sphere::Sphere
sphere.metric = ... implementation ...
sphere.metric(... arguments ...)

Nevertheless, this is probably not efficient at the moment, since
there is a performance penalty for anonymous functions. You might try
an approach like this instead, which is as flexible as the one above,
but more efficient:

immutable Sphere <: Manifold
    dim::Int
    metric
end

# Introduce an empty new type for the metric function
immutable Metric1 end
call(::Metric1, ... other arguments ...) = ... implementation ...

This is equivalent to using an anonymous function, but should be
currently more efficient.

Then you can write e.g.

sphere::Sphere
sphere.metric(... arguments ...)

-erik

On Mon, Jan 18, 2016 at 11:54 AM, Anonymous <[email protected]> wrote:
> This came up as I was trying to define a sphere manifold type which can have
> distinct metric structures put on it, here is how I have to do it in Julia:
>
> abstract Manifold
> abstract Metric
>
> immutable Metric1 <: Metric
> end
>
> immutable Metric2 <: Metric
> end
>
> immutable Sphere{T<:Metric} <: Manifold
> dim::int
> end
>
> metric(M::Sphere{Metric1}, x1, x2) = ...
> metric(M::Sphere{Metric2}, x1, x2) = ...
>
> As you can see, I have to define a whole other type tree just so my metric
> function can distinguish between sphere manifolds depending on what metric
> structure I want it to have.  It would be both simpler and make more sense
> conceptually to attach the metric to the manifold itself when I instantiate
> it.  Of course you might say I should just define two functions metric1 and
> metric2, but this doesn't really make sense either because these metrics do
> not cut across multiple manifolds, and thus there would be no way to take
> advantage of the multiple dispatch functionality.
>
>



-- 
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to