Your Hello World program prints to the Transcript similar to a console. To open the Transcript, left-click on open space to bring up the World menu, select Tools, and then select Transcript.
Finally, to run your program, you need to open the Playground. To open the Playground, left-click on open space to bring up the World menu, then select Playground. In Playground, enter the following text:. Smalltalk's syntax is based almost entirely on message passing : sending messages to objects. Even its control structures like conditionals and loops are implemented using messages.
This makes the language so simple that its complete syntax fits on a post card! There are three kinds of messages: unary, binary, and keyword which takes arguments. The object is specified first, followed by the message.
Object-oriented programming - Wikiwand
The precedence rule says unary messages are performed first, then binary, then keyword. Otherwise, operations are performed from left to right. This can make arithmetic look slightly odd:. In Smalltalk, the comma operator performs string concatenation. The cr message inserts carriage return. This means that a Smalltalk program is able to inspect its own structure and computation at runtime.
Computational reflection is used to implement a powerful way to handle errors. There are many things that the program can do with the doesNotUnderstand: message, including extending itself with new functionality! The Smalltalk image allows you to save the execution state of your program at any time and resume execution later on from exactly where you left off!
- Lung Cancer Therapy Annual: 2.
- SmallTalk and Object Orientation: An Introduction by John Hunt.
- The Dead Rabbit Drinks Manual: Secret Recipes and Barroom Tales from Two Belfast Boys Who Conquered the Cocktail World.
Image persistence can be used as a kind of database where all the data in your program is persisted to hard drive. You can alter your program while it's running! Live coding and debugging is a powerful way to program and is the principal reason for Smalltalk's tremendous productivity. This makes Smalltalk a functional language, as well, except that it doesn't have immutability. Pharo now supports immutability. Now our first test almost passes. For these we will make a major departure from the Smalltalk model, which has a fairly complex metaclass system.
Instead we will use the model introduced in ObjVlisp 1 , which Python adopted. By default, every class is an instance of TYPE. However, the programmer can also subclass TYPE to make a new metaclass:. To define new metaclasses, it is enough to subclass TYPE. However, in the rest of this chapter we won't do that; we'll simply always use TYPE as the metaclass of every class.
Now the first test passes. The second test checks that reading and writing attributes works on classes as well.
It's easy to write, and passes immediately. So far we haven't taken advantage of the fact that objects have classes. The next test implements the isinstance machinery:. To check whether an object obj is an instance of a certain class cls , it is enough to check whether cls is a superclass of the class of obj , or the class itself. To check whether a class is a superclass of another class, the chain of superclasses of that class is walked. If and only if the other class is found in that chain, it is a superclass.
The chain of superclasses of a class, including the class itself, is called the "method resolution order" of that class.
It can easily be computed recursively:. The remaining missing feature for this first version of the object model is the ability to call methods on objects. In this chapter we will implement a simple single inheritance model. To find the correct implementation of a method that is sent to an object, we walk the method resolution order of the class of the object.
Smalltalk and Object Orientation
The first method found in the dictionary of one of the classes in the method resolution order is called:. Together with the code for callmethod in the Base implementation, this passes the test. To make sure that methods with arguments work as well, and that overriding of methods is implemented correctly, we can use the following slightly more complex test, which already passes:.
The result of that lookup operation is a bound method , an object that encapsulates both the object as well as the function found in the class. Next, that bound method is called with a call operation 2. To implement this behaviour, we need to change the Base. If the attribute is not found in the dictionary, it is looked for in the class. If it is found in the class, and the attribute is a callable, it needs to be turned into a bound method. To emulate a bound method we simply use a closure.
In addition to changing Base.
Smalltalk: The Original Object-Oriented Programming Language?
In addition to "normal" methods that are called directly by the program, many dynamic languages support special methods. These are methods that aren't meant to be called directly but will be called by the object system. In Python those special methods usually have names that start and end with two underscores; e. Special methods can be used to override primitive operations and provide custom behaviour for them instead. Thus, they are hooks that tell the object model machinery exactly how to do certain things. Python's object model has dozens of special methods. That is also where the name meta-object protocol , for collections of special methods, was coined 3.
In this chapter we will add three such meta-hooks to our object model.
They are used to fine-tune what exactly happens when reading and writing attributes. It gets the name of the attribute being looked up as an argument. This base implementation simply does what setting an attribute did so far, which is write the attribute into the object's dictionary. To pass these tests, the Base. With these modifications, the new test passes. To get around this, the descriptor protocol was introduced in Python.
About this book
It can be seen as the generalization of binding a method to an object — and indeed, binding a method to an object is done using the descriptor protocol. In addition to bound methods, the most important use case for the descriptor protocol in Python is the implementation of staticmethod , classmethod and property. In this subsection we will introduce the subset of the descriptor protocol which deals with binding objects.
Implementing this behaviour is easy. This makes the test pass. In practice, the descriptor protocol is quite a lot more complex. Also, the current implementation is cutting a few corners. This is necessary since our object model borrows functions and thus methods from Python, instead of having a representation for them that uses the object model.
A more complete object model would have to solve this problem. While the first three variants of the object model were concerned with behavioural variation, in this last section we will look at an optimization without any behavioural impact. Jonathan Rees collected a list of nine properties of OO-languages. Subsets are often used for a definition, yet no programming language implements all properties.
An interesting game is to remove properties by finding a language that is considered object-oriented, yet doesn't use a property.