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?
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) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With