Content deleted Content added
No edit summary |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 38:
A Citrine program is basically a sequence of messages sent to objects. For instance, to determine whether 5 is an even number, the message 'even?' is sent to the number 5.
<
5 even?
</syntaxhighlight>
This is called a ''unary'' message because it takes no arguments. A ''binary'' message is always a single UTF-8 character; this differs from Smalltalk, where there is a fixed set of binary messages. Here is an example:
<
6 + 7.
</syntaxhighlight>
Here a binary message '+' is sent to number 6, the argument of this binary message is '7', this will result in a new number object '13'. Assigning the outcome of this operation to a variable uses the assignment operator: :=.
<
total := money + debt.
</syntaxhighlight>
Also note that each line in a Citrine program ends with a dot, just like in Smalltalk. Besides unary and binary messages, Citrine offers ''keyword messages'', which take arguments interspersed with the message itself just like Smalltalk and [[Objective-C]].
<
☞ x := Number between: 1 and: 5.
</syntaxhighlight>
The code snippet above will return a boolean object ''True''.
Line 65:
Just like Smalltalk, control flow in Citrine is implemented by strategic use of messages. For instance, writing a conditional statement requires sending a block of code to a boolean.
<
(money > price) true: { ✎ write: 'Yes, you can afford this'. }.
</syntaxhighlight>
Likewise, a for-loop is written as:
<
{ :step ✎ write: 'this is step:' + step. } * 10.
</syntaxhighlight>
To break out of a loop in Citrine one has to send the message 'break' to a boolean, this allows a conditional break from a loop without having to factor out the break conditions:
<
{ :i
(i = 3) break.
✎ write: i.
} * 5.
</syntaxhighlight>
==Pipelines==
Unlike Smalltalk, Citrine has no semi-colon to send a message to the original receiver. Instead Citrine has a comma token ',' used to chain keyword messages, this allows writing Unix-like ''[[Pipeline (Unix)|pipelines]]''. The following code uses a pipeline-like syntax to replace all the 'o' characters with zeroes, the resulting string would be something like: '1010101...'.
<
onesAndZeroes := '1o1o1o1o1o1' split: 'o', map: mapUp, join: '0'.
</syntaxhighlight>
==Prototypes==
The biggest difference from Smalltalk is the use of prototypes. Citrine does not have a concept of a class, it only knows about objects. An object is created using the new message:
<
cat := Object new.
</syntaxhighlight>
This object can be made to respond to messages by ordering the object to listen to events. This is kind of similar to
adding methods in languages like [[Java (programming language)|Java]]:
<
cat on: 'meow' do: {
Pen write: 'meow!'.
}.
</syntaxhighlight>
As stated above, inheritance is based on prototypes. To derive an object from another object, the new message must be sent to the object to be extended:
<
☞ Animal := Object new.
Animal on: 'makeSound' do: {
Line 121:
☞ Tom := Cat new.
Tom makeSound.
</syntaxhighlight>
==Unicode==
Citrine uses UTF-8 unicode extensively, both objects and messages can consist of unicode symbols. All string length are calculated using UTF-8. Citrine distinguishes string length and size in bytes:
<
'text' length.
</syntaxhighlight>
returns the length of the string in UTF-8 code pointes, while:
<
'text' bytes.
</syntaxhighlight>
returns the number of bytes.
Line 143:
The following demonstration makes the Mailer object available in the module:
<
Application := {
☞ mailer := Mailer new.
module run.
}.
</syntaxhighlight>
==See also==
|