28

In the Io Language, there are 2 methods for creating slots: newSlot and setSlot. Both seem to have similar behavior except newSlot also creates a setter. What cases are there for a needing a setter to be created at the same time as slot creation? What exactly is the purpose of the setter anyway?

Dan
  • 710
  • 5
  • 15
  • 3
    I was also wondering what a setter was at the end of the first day of Io in 7 languages in 7 weeks. – Jedidja Oct 18 '12 at 20:20

1 Answers1

35

I believe its a convenience which provides good coding practises. Thus if you want to expose an objects attribute then newSlot or its synonym ::= are the preferred way to go.

newSlot can make things look nicer. For eg.

Animal := Object clone do (
    legs ::= nil    // creates leg slot  & setLegs() setter
    tail ::= nil    // creates tail slot & setTail() setter
)

// I think below is more aesthetic 
Cat := Animal clone setLegs(4) setTail(1)

// compared to this
Dog := Animal clone do (legs = 4; tail = 1)

And also it can get around do() context. For eg.

Pet := Animal clone do (
    name ::= nil
)

myPetCats := list("Ambrose", "Fluffy", "Whiskers") map (petName,
    Pet clone do (name = petName)   // throws exception
)

The Pet clone do (name = petName) will die throwing Exception: Pet does not respond to 'petName' because do() is interpreted within the cloned Pet context and so it cannot see petName.

So instead you need to use the setter:

myPetCats := list("Ambrose", "Fluffy", "Whiskers") map (petName,
    Pet clone setName(petName)
)
AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71