[julia-users] Re: Managing objects and state
If you want to use julia functions like OOP methods just make the first argument the object instance so function setx!(s::State, i::int) #modifies s, so we use "!" in function name s.x = i end function squarex(s::State) return s.x^2 end #main code s = State() setx!(s,2) #s.x == 2 y =squarex(s) #y == 4 If you adopt the convention that the "object" is the first argument to the function and the later arguments are the parameters of the method, s.method(param) translates to function(s, param). So your zero argument functions that modify program state become one argument functions that modify the state variable that is passed to them. Keep in mind the well loved Julia convention that functions which modify their arguments should end in "!" like "setx!(s::State)" or "increment!(c::Counter)". Does this make things easier to reason about? James
[julia-users] Re: Managing objects and state
Thanks. So the type instance is passed in as a parameter. My problem is that if this is a remote call I can't see how to handle the state without having a global instance of State. Something like this so I first remote call somefunction() which sets some state and then remote call anotherfunction() which adds the parameter before it calls the realanotherfunction(). Having been warned away from using globals this seems a bad solution. Is there another way to do this? type State x::int end s = State function somefunction() ... s.x = 1 ... end function anotherfunction() realanother(s) end realanotherfunction(s::State) ... s.x = 10 ... end On Monday, March 24, 2014 7:32:41 PM UTC, Bob Cowdery wrote: > > Forgive me if this is daft and often answered question but I'm trying to > get my head around Julia OO. > > Am I correct in thinking that as there is no binding between methods and > types that I have to explicitly pass in an instance of a type to a method. > If that is the case then how do I hold state on a remote object. I call a > function that creates an instance of a type say something simple. > type State > > sock > > end > I just want to hold a socket reference. If the first remote function I call > creates State then how can the next function I call obtain the state instance. > > Do I have to create a remote reference or something which it can use? > > Thanks > Bob > > >
[julia-users] Re: Managing objects and state
it's not completely clear to me what you're asking. julia isn't particularly object oriented (i just checked and there's no mention of objects or classes in the intro at http://julialang.org/) having said that, you can store and modify (unless the type is immutable) values in composite types. so, for example julia> type State x::Int end julia> function set(s::State, x::Int) s.x = x end set (generic function with 1 method) julia> function read(s::State) return s.x end read (generic function with 1 method) julia> s = State(12) State(12) julia> s.x 12 julia> s.x = 100 100 julia> s.x 100 julia> s State(100) julia> set(s, 32) 32 julia> s State(32) julia> read(s) 32 maybe that helps? obviously set and read are trivial to do directly on the instance, but you could imagine more complex functions. andrew On Monday, 24 March 2014 16:32:41 UTC-3, Bob Cowdery wrote: > > Forgive me if this is daft and often answered question but I'm trying to > get my head around Julia OO. > > Am I correct in thinking that as there is no binding between methods and > types that I have to explicitly pass in an instance of a type to a method. > If that is the case then how do I hold state on a remote object. I call a > function that creates an instance of a type say something simple. > type State > > sock > > end > I just want to hold a socket reference. If the first remote function I call > creates State then how can the next function I call obtain the state instance. > > Do I have to create a remote reference or something which it can use? > > Thanks > Bob > > >