learnxinyminutes-docs/self.html.markdown

162 lines
4.1 KiB
Markdown
Raw Normal View History

2014-10-27 14:07:24 +03:00
---
language: self
2014-10-29 06:46:27 +03:00
contributors:
2014-10-27 14:07:24 +03:00
- ["Russell Allen", "http://github.com/russellallen"]
2014-10-31 01:44:24 +03:00
filename: learnself.self
2014-10-27 14:07:24 +03:00
---
2015-10-08 06:11:24 +03:00
Self is a fast prototype based OO language which runs in its own JIT vm. Most development is done through interacting with live objects through a visual development environment called *morphic* with integrated browsers and debugger.
2014-10-27 14:07:24 +03:00
Everything in Self is an object. All computation is done by sending messages to objects. Objects in Self can be understood as sets of key-value slots.
# Constructing objects
2015-10-08 06:11:24 +03:00
The inbuild Self parser can construct objects, including method objects.
2014-10-27 14:07:24 +03:00
```
"This is a comment"
"A string:"
'This is a string with \'escaped\' characters.\n'
"A 30 bit integer"
23
"A 30 bit float"
3.2
"-20"
-14r16
"An object which only understands one message, 'x' which returns 20"
(|
x = 20.
|)
"An object which also understands 'x:' which sets the x slot"
(|
x <- 20.
|)
2015-10-08 06:11:24 +03:00
"An object which understands the method 'doubleX' which
2014-10-29 06:46:27 +03:00
doubles the value of x and then returns the object"
2014-10-27 14:07:24 +03:00
(|
x <- 20.
doubleX = (x: x * 2. self)
|)
2015-10-08 06:11:24 +03:00
"An object which understands all the messages
that 'traits point' understands". The parser
looks up 'traits point' by sending the messages
'traits' then 'point' to a known object called
the 'lobby'. It looks up the 'true' object by
2014-10-29 06:46:27 +03:00
also sending the message 'true' to the lobby."
2014-10-27 14:07:24 +03:00
(| parent* = traits point.
x = 7.
y <- 5.
isNice = true.
|)
```
# Sending messages to objects
2016-02-23 06:27:17 +03:00
Messages can either be unary, binary or keyword. Precedence is in that order. Unlike Smalltalk, the precedence of binary messages must be specified, and all keywords after the first must start with a capital letter. Messages are separated from their destination by whitespace.
2014-10-27 14:07:24 +03:00
```
2015-10-08 06:11:24 +03:00
"unary message, sends 'printLine' to the object '23'
2014-10-27 14:11:20 +03:00
which prints the string '23' to stdout and returns the receiving object (ie 23)"
23 printLine
"sends the message '+' with '7' to '23', then the message '*' with '8' to the result"
2015-10-08 06:11:24 +03:00
(23 + 7) * 8
2014-10-27 14:11:20 +03:00
"sends 'power:' to '2' with '8' returns 256"
2015-10-08 06:11:24 +03:00
2 power: 8
2014-10-27 14:11:20 +03:00
2015-10-08 06:11:24 +03:00
"sends 'keyOf:IfAbsent:' to 'hello' with arguments 'e' and '-1'.
2014-10-27 14:11:20 +03:00
Returns 1, the index of 'e' in 'hello'."
2015-10-08 06:11:24 +03:00
'hello' keyOf: 'e' IfAbsent: -1
2014-10-27 14:07:24 +03:00
```
# Blocks
Self defines flow control like Smalltalk and Ruby by way of blocks. Blocks are delayed computations of the form:
```
2014-10-29 06:46:27 +03:00
[|:x. localVar| x doSomething with: localVar]
2014-10-27 14:07:24 +03:00
```
Examples of the use of a block:
```
2014-10-27 14:13:08 +03:00
"returns 'HELLO'"
2015-10-08 06:11:24 +03:00
'hello' copyMutable mapBy: [|:c| c capitalize]
2014-10-27 14:07:24 +03:00
2014-10-27 14:13:08 +03:00
"returns 'Nah'"
2015-10-08 06:11:24 +03:00
'hello' size > 5 ifTrue: ['Yay'] False: ['Nah']
2014-10-27 14:07:24 +03:00
2014-10-27 14:13:08 +03:00
"returns 'HaLLO'"
2015-10-08 06:11:24 +03:00
'hello' copyMutable mapBy: [|:c|
2014-10-27 14:07:24 +03:00
c = 'e' ifTrue: [c capitalize]
False: ['a']]
```
Multiple expressions are separated by a period. ^ returns immediately.
```
2014-10-27 14:13:08 +03:00
"returns An 'E'! How icky!"
2015-10-08 06:11:24 +03:00
'hello' copyMutable mapBy: [|:c. tmp <- ''|
2014-10-27 14:07:24 +03:00
tmp: c capitalize.
tmp = 'E' ifTrue: [^ 'An \'E\'! How icky!'].
c capitalize
]
```
Blocks are performed by sending them the message 'value' and inherit (delegate to) their contexts:
```
2014-10-27 14:13:08 +03:00
"returns 0"
2014-10-27 14:07:24 +03:00
[|x|
x: 15.
"Repeatedly sends 'value' to the first block while the result of sending 'value' to the
second block is the 'true' object"
2015-10-08 06:11:24 +03:00
[x > 0] whileTrue: [x: x - 1].
2014-10-27 14:07:24 +03:00
x
2014-10-27 14:11:20 +03:00
] value
2014-10-27 14:07:24 +03:00
```
# Methods
2014-10-29 06:46:27 +03:00
Methods are like blocks but they are not within a context but instead are stored as values of slots. Unlike Smalltalk, methods by default return their final value not 'self'.
2014-10-27 14:07:24 +03:00
```
2014-10-27 14:13:08 +03:00
"Here is an object with one assignable slot 'x' and a method 'reduceXTo: y'.
2015-10-08 06:11:24 +03:00
Sending the message 'reduceXTo: 10' to this object will put
2014-10-27 14:13:08 +03:00
the object '10' in the 'x' slot and return the original object"
2015-10-08 06:11:24 +03:00
(|
2014-10-27 14:07:24 +03:00
x <- 50.
reduceXTo: y = (
2015-10-08 06:11:24 +03:00
[x > y] whileTrue: [x: x - 1].
2014-10-27 14:07:24 +03:00
self)
|)
2014-10-27 14:13:08 +03:00
.
2014-10-27 14:07:24 +03:00
```
# Prototypes
Self has no classes. The way to get an object is to find a prototype and copy it.
```
| d |
d: dictionary copy.
d at: 'hello' Put: 23 + 8.
d at: 'goodbye' Put: 'No!.
"Prints No!"
( d at: 'goodbye' IfAbsent: 'Yes! ) printLine.
"Prints 31"
( d at: 'hello' IfAbsent: -1 ) printLine.
```
# Further information
The [Self handbook](http://handbook.selflanguage.org) has much more information, and nothing beats hand-on experience with Self by downloading it from the [homepage](http://www.selflanguage.org).