On Sunday, November 30, 2014 6:40:31 PM UTC+10, 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 >
If you look at the section of the manual above this you will find a discussion of multiple dispatch. This is really the idiomatic approach in Julia. So methods are defined outside the type definition (since they need not "belong" to any one type). What Julia doesn't provide is what has been called "dot oriented programming" (TM Jeff?) where there is a specific syntax to associate a "receiver" with a function. Again thats because methods don't "belong" to any one type. Cheers Lex > 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() > > >
