On Mon, Jan 18, 2016 at 10:08 AM, Anonymous <[email protected]> wrote:
> Is the following code considered bad form in Julia?

Yes.

>
> immutable Foo
> func::Function
> end
>
> foo = Foo(x->x^2)
> foo.func(3)
>
> This mimics the behavior of OOP since just like in OOP the internal method
> cannot be changed (since the type is immutable).  Sometimes it really does
> make the most sense to attach a function to an instance of a type, do I take
> a performance hit doing things this way?

You do, by a huge amount.

What I've tried and kind of works is to do basically

immutable FooFunc{T}
    self::T
end

type Foo
    func::FooFunc{Foo}
    Foo() = (self = new(); self.func = FooFunc{Foo}(self); self)
end

call(func::FooFunc{Foo}, x) = x^2

This can be simpler with the closure improvement and getfield overload.

Reply via email to