Hacker News new | past | comments | ask | show | jobs | submit login

You don't. See http://wcook.blogspot.com/2012/07/proposal-for-simplified-mo... by William Cook. However, objects help to tame mutable state.



Can you point me to a language or some code snippets with objects with no state --- in my head this just comes back to some sort of namespace for static functions. At that point, you need no object, so what's the point of having one. If you are saying that having a reference to an object is not 'state' then OK, but that makes no sense to me. I'm not trying to be pedantic, just trying to understand if OP had a typo or there is a whole world out there that I can't conceive of.


Again, in discussions like this state is often used to refer to mutable state (an identical thing itself mutates).

Having a conceptual reference (oh god another overloaded term) to something does not imply that there is mutable state on the programming language as long as the reference is immutable.

    def times(x, y)
      if x.is_one?
        y
      elsif x.is_zero?
        x
      else
        y.add(times(x.decrement, y)
      end
    end
a) x and y are not mutated. b) you can implement the called methods on various immutable object types, and the code would work (assuming we send in the same types, and they obey certain protocols but that is yet another different concern):

    class WeirdString
      def init(v)
        @val = v
      end

      def val
        @val
      end

      def is_zero?
        @val.empty?
      end

      def is_one?
        @val.length == 1
      end

      def decrememt
        WeirdString.new(@val[0..-2])
      end

      def add(ws)
        WeirdString.new(@val + ws.val)
    end

    # times(WeirdString.new('abc', 'hi').val → 'hihihi'
Now, you may say the fact that the object has an instance variable means there is state and you'd be right depending on how you define state, but at any rate the state of a given instance of WeirdString is not mutable.


If that's what you mean by "no state", then I misunderstood, but I don't especially care what you call a namespace for static functions. (A module?) I use immutable objects frequently, and they're a different thing, just as first-class nested functions are a different thing from standard C functions.


IMO that post is specifically calling out the mutable part, not claiming to have objects completely without state.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: