Self (programming language)
Self is a general-purpose, high-level, object-oriented programming language based on the concept of prototypes.
≈ 7 min read
Self (programming language). encyclopegia.org, snapshot enwiki-001369059645. /a/self-programming-language
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Self is a general-purpose, high-level, object-oriented programming language based on the concept of prototypes. Self began as a dialect of Smalltalk, being dynamically typed and using just-in-time compilation (JIT) with the prototype-based approach to objects: it was first used as an experimental test system for language design in the 1980s and 1990s.
In 2006, Self was still being developed as part of the Klein project, which was a Self virtual machine written fully in Self. The latest version, 20241.1 was released in August 2024.
Several just-in-time compilation techniques were pioneered and improved in Self research as they were required to allow a very high-level object-oriented language to perform at up to half the speed of optimized C. Much of the development of Self took place at Sun Microsystems, and the techniques they developed were later deployed for Java's HotSpot virtual machine. At one point a version of Smalltalk was implemented in Self.
Because it was able to use the JIT, this also gave extremely good performance. myPerson name returns the value in name, and myPerson name:'foo' sets it. Self, like Smalltalk, uses blocks for flow control and other duties.
Methods are objects containing code in addition to slots (which they use for arguments and temporary values), and can be placed in a Self slot just like any other object: a number for example. The syntax remains the same in either case. Note that there is no distinction in Self between fields and methods: everything is a slot.
Since accessing slots via messages forms the majority of the syntax in Self, many messages are sent to self, and the self can be left off (hence the name). 'Hello, World!' print. This is the Self version of the "Hello, World!" program.
The ' syntax indicates a literal string object. Other literals include numbers, blocks and general objects. Grouping can be forced by using parentheses.
In the absence of explicit grouping, the unary messages are considered to have the highest precedence followed by binary (grouping left to right) and the keywords having the lowest. The use of keywords for assignment would lead to some extra parenthesis where expressions also had keyword messages, so to avoid that Self requires that the first part of a keyword message selector start with a lowercase letter, and subsequent parts start with an uppercase letter. valid: base bottom between: ligature bottom + height And: base top / scale factor. can be parsed unambiguously, and means the same as: valid: ((base bottom) between: ((ligature bottom) + height) And: ((base top) / (scale factor))).
In Smalltalk-80, the same expression would be written as: valid := self base bottom between: self ligature bottom + self height and: self base top / self scale factor. assuming base , ligature , height and scale were not instance variables of self but were, in fact, methods. labelWidget copy label: 'Hello, World!'. makes a copy of the "labelWidget" object with the copy message (no shortcut this time), then sends it a message to put "Hello, World" into the slot called "label". Now to do something with it: (desktop activeWindow) draw: (labelWidget copy label: 'Hello, World!').
In this case the (desktop activeWindow) is performed first, returning the active window from the list of windows that the desktop object knows about. Next (read inner to outer, left to right) the code we examined earlier returns the labelWidget. Finally the widget is sent into the draw slot of the active window.
Traits
Making a clone of this object for "Bob's account" will create a new object which starts out exactly like the prototype. In this case we have copied the slots including the methods and any data. However a more common solution is to first make a more simple object called a traits object which contains the items that one would normally associate with a class.
In this example the "bank account" object would not have the deposit and withdraw method, but would have as a parent an object that did. In this way many copies of the bank account object can be made, but we can still change the behaviour of them all by changing the slots in that root object. How is this any different from a traditional class?
Well consider the meaning of: myObject parent: someOtherObject. This excerpt changes the class of myObject at runtime by changing the value associated with the 'parent*' slot (the asterisk is part of the slot name, but not the corresponding messages).
Unlike with inheritance or lexical scoping, the delegate object can be modified at runtime. _AddSlots: (| vehicle Since the receiver of the '_AddSlots:' primitive isn't indicated, it is self. In the case of expressions typed at the prompt, that is an object called the "lobby". The argument for '_AddSlots:' is the object whose slots will be copied over to the receiver.
In this case it is a literal object with exactly one slot. The slot's name is 'vehicle' and its value is another literal object. The notation implies a second slot called 'vehicle:' which can be used to change the first slot's value.
The = indicates a constant slot, so there is no corresponding 'parent:'. The literal object that is the initial value of 'vehicle' includes a single slot so it can understand messages related to cloning.
A truly empty object, indicated as (| |) or more simply as (), cannot receive any messages at all. vehicle _AddSlots: (| name Here the receiver is the previous object, which now will include 'name' and 'name:' slots in addition to 'parent*'. _AddSlots: (| sportsCar Though previously 'vehicle' and 'sportsCar' were exactly alike, now the latter includes a new slot with a method that the original doesn't have. Methods can only be included in constant slots. _AddSlots: (| porsche911 The new object 'porsche911' started out exactly like 'sportsCar', but the last message changed the value of its 'name' slot.
Note that both still have exactly the same slots even though one of them has a different value.
Performance
Self VMs achieved performance of approximately half the speed of optimised C on some benchmarks. This was achieved by just-in-time compilation techniques which were pioneered and improved in Self research to make a high level language perform this well.
Garbage collection
The garbage collector for Self uses generational garbage collection which segregates objects by age. By using the memory management system to record page writes a write-barrier can be maintained. This technique gives excellent performance, although after running for some time a full garbage collection can occur, taking considerable time.
Optimizations
The run time system selectively flattens call structures. This gives modest speedups in itself, but allows extensive caching of type information and multiple versions of code for different caller types. This removes the need to do many method lookups and permits conditional branch statements and hard-coded calls to be inserted- often giving C-like performance with no loss of generality at the language level, but on a fully garbage collected system.
See also
Morphic (software) Cecil (programming language)
Further reading
Published papers on Self Series of four articles "Environment and the programming language Self"
External links
Former Self Home Page at Sun Microsystems
Text derived from https://en.wikipedia.org/w/index.php?oldid=1369059645 under CC BY-SA 4.0; modifications have been made.
References
- ↩Wikipedia “Self (programming language)” release date (×1) · confidence 0.95 revision URL