(By the way, the manual you referenced is for julia v 0.2, which is 
outdated. The current version is here: 
http://julia.readthedocs.org/en/release-0.3/manual/.

One way would be to define an inner constructor 
<http://julia.readthedocs.org/en/release-0.3/manual/constructors/#inner-constructor-methods>
:

type Foo
    x::Number
    y::Number
    Compute::Function
    function Foo(x,y)
        f = new(x, y)
        f.x=x
        f.y=y
        f.Compute = function() f.x+f.y end
        return f
    end
end

This is not ideomatic julia however. The julian way would be to define a 
function called Compute with a method for Foo types.

type Foo
    x::Number
    y::Number
end

Compute(f::Foo) = f.x + f.y

If you have some other type that needs a Compute method, you would also 
define it for that type:

Compute(x::Float64) = x+x

for example.

On Sunday, November 30, 2014 12:40:31 AM UTC-8, Sean Gerrish wrote:
>
> Hi Julia-users,
>   I'd find it natural to attach a method to a particular instance of an 
> object as in traditional object-oriented programming (see the example 
> below).  I can manage this with the constructor NewFoo, which binds the 
> instance to the method after the object is created.  Is this idiomatic in 
> Julia?  Is there a more idiomatic way?  The most I could find in Julia 
> documentation on the subject was this footnote: 
> http://docs.julialang.org/en/release-0.2/manual/methods/#id1
>
> Thanks in advance,
> Sean
>
>
> type Foo
>     x::Number
>     y::Number
>     Compute::Function
> end
>
> function NewFoo(x, y)
>     f = Foo(x, y, function() 0 end)
>     f.Compute = function() f.x + f.y end
>     f
> end
>
> foo = NewFoo(2, 3)
> foo.Compute()
>
>
>

Reply via email to