Citrine (programming language): Difference between revisions

Content deleted Content added
Fixed syntax error.
m {{code}}, lang="smalltalk"
Line 23:
Citrine has a very limited syntax and it's very closely related to Smalltalk. Everything in Citrine is an ''object'', there are 5 literals:
 
* {{code|Nil}}
<source lang="citrine">
* {{code|True, False}}
* Nil
* {{code|0,1,2,3}}
* True False
* {{code|'String'}}
* 0,1,2,3
* <code>{ param | ...block of code... }</code>
* 'String'
* { param | ...block of code... }
</source>
 
The code block literal uses a ''pipe'' symbol to separate the parameters from the logic '|', if there are no
Line 38 ⟶ 36:
A Citrine program is basically a sequence of messages sent to objects. For instance, to calculate the factorial of the number 5, the message 'factorial' is sent to the number 5.
 
<source lang="citrinesmalltalk">
5 factorial.
</source>
Line 44 ⟶ 42:
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:
 
<source lang="citrinesmalltalk">
6 + 7.
</source>
Line 50 ⟶ 48:
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: :=.
 
<source lang="citrinesmalltalk">
total := money + debt.
</source>
Line 56 ⟶ 54:
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]].
 
<source lang="citrinesmalltalk">
3 between: 1 and: 5.
</source>
Line 65 ⟶ 63:
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.
 
<source lang="citrinesmalltalk">
(money >= price) ifTrue: {\ Pen write: 'Yes, you can afford this'. }.
</source>
Line 71 ⟶ 69:
Likewise, a for-loop is written as:
 
<source lang="citrinesmalltalk">
1 to: 10 step: 1 do: { step | Pen write: 'this is step:' + step. }.
</source>
Line 77 ⟶ 75:
Besides for-loops, Citrine features each-loops, while-loops and a simple times loop, the latter uses the binary message '*'.
 
<source lang="citrinesmalltalk">
7 * { step | Pen write: 'this is step:' + step. }.
</source>
Line 83 ⟶ 81:
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:
<source lang="citrinesmalltalk">
#0-4
0 to: 10 step: 1 do: { i |
Line 94 ⟶ 92:
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...'.
 
<source lang="citrinesmalltalk">
onesAndZeroes := '1o1o1o1o1o1' split: 'o', map: mapUp, join: '0'.
</source>
Line 101 ⟶ 99:
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:
 
<source lang="citrinesmalltalk">
cat := Object new.
</source>
Line 108 ⟶ 106:
adding methods in languages like [[Java (programming language)|Java]]:
 
<source lang="citrinesmalltalk">
cat on: 'meow' do: {\
Pen write: 'meow!'.
Line 116 ⟶ 114:
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:
 
<source lang="citrinesmalltalk">
Animal := Object new.
Animal on: 'makeSound' do: {\
Line 132 ⟶ 130:
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:
 
<source lang="citrinesmalltalk">
'text' length.
</source>
Line 138 ⟶ 136:
returns the length of the string in UTF-8 code pointes, while:
 
<source lang="citrinesmalltalk">
'text' bytes.
</source>
Line 149 ⟶ 147:
Demonstration:
 
<source lang="citrinesmalltalk">
Application := {\
var mailer := Mailer new. #make mailer available to all modules