Interchangeable state and behavior

Sender does not distinguish between state and behavior; implementor can choose as desired.
Stock-based account:

Benefits of interchangeable state and behavior

Another important property of Self is that when we send a message we do not distinguish whether that message activates a method or accesses a data slot. We can change our implementation between state and behavior at will.

As an example, let's make a new kind of account based on shares of stock. We'll do this by modifying an existing bank account.

First, we add slots to represent the current stock price, and the number of shares held. Adding slots is accomplished using the middle-button menu, and the syntax we use for the current stock price slot is this:

	pricePerShare <- 50.0
 

This is the syntax for the slot containing the number of shares in the account:

	numShares <- 10 

The use of the <- indicates that the slot is to be an assignable data slot, and the value to the right is the initial value of the slot.

The balance slot that remains is now incorrect: the balance should be computed from the stock held. We can edit this slot, using the middle-button menu item, `Edit', and replace it with a method slot:

	balance = (numShares * pricePerShare) 

If we want to set the balance directly, we can add a method slot balance:, like this:

	balance: b = (numShares: b / pricePerShare) 

[ Previous ] [ Index ] [ Next ]