Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between newSlot and setSlot in the Io Language?

Tags:

iolanguage

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?

like image 665
Dan Avatar asked May 12 '11 01:05

Dan


1 Answers

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) ) 
like image 83
draegtun Avatar answered Sep 20 '22 21:09

draegtun