Thank you for the quick reply.
With this example it makes much sense:
Depending on the language you will have to adopt a different style of
programming.
That makes much sense ;-)
On Wednesday, August 10, 2016 at 9:46:26 AM UTC+2, Mauro wrote:
>
> Yes, this is correct. The difference to classic OO programming
> languages is that in Julia a method is not "owned" by a type. Instead
> it can be owned by several types as dispatch is on all arguments. In
> python and other OO dispatch is only on the first (usually implicit)
> argument. For your example this does not really matter, but if you have
> several "equal" types interacting, then it does:
>
> type Rocket end
> type Asteroid end
> type Planet end
>
> collide(a,b) = collide(b,a) # make it commute
> collide(::Rocket, ::Union{Asteroid,Planet}) = println("rocket explodes")
> collide(::Asteroid, ::Planet) = println("dinosaurs die")
>
> collide(Planet(),Rocket()) # rocket explodes
> collide(Asteroid(), Planet()) # dinosaurs die
>
> This would be more awkward to program in python. I think it's a better
> mental model to not view Julia as OO.
>
> On Wed, 2016-08-10 at 09:26, Willem Hekman <[email protected]
> <javascript:>> wrote:
> > Hello all,
> >
> > I must say that I`m quite new to object oriented programming.
> >
> > Do I understand correctly from the manual that in Julia (unlike python)
> you
> > do not use the keyword "self" and declare methods that apply to a type
> > outside the type definition?
> >
> > To illustrate, let's say we want to have a type of apple and want to
> push a
> > flavor to the array of flavors that characterizes an apple:
> >
> > # define a type: Apple
> > type Apple
> > brand::ASCIIString
> > color::ASCIIString
> > flavors::Array{ASCIIString,1}
> >
> >
> > end
> >
> > # a method designed to add flavors to the apple
> > function add_flavor(apple::Apple,flavor::ASCIIString)
> >
> > push!(apple.flavors,flavor)
> > end
> > # create an instance of an AppleFuji = Apple("Fuji","red",["sweet"])
> >
> > # add a flavor
> > add_flavor(Fuji, "sour")
> >
> > Is this the way you'd do it in Julia?
> >
> > In python I got used to putting methods that apply to "Apple" instances
> > inside the type definition where the keyword "self" would be used to add
> a
> > flavor: push!(self.flavors,flavor)
> >
> > What would you say?
> >
> > -Willem
>