Content deleted Content added
→Hello World example: use write() instead of show(). Show would display the quotes, unlike traditional K&R example. |
m <pre> for unsupported lang |
||
Line 29:
===Assignments===
Magik uses the <code><<</code> operator to make [[Assignment (computer science)|assignments]]:
<pre>
a << 1.234
b << b + a
c << "foo" + "bar" # Concat strings
</
For clarity, this notation is read as "a becomes 1.234" or "b becomes b plus a". This terminology separates assignment from [[Comparison (computer programming)|comparison]].
Magik also supports a compressed variation of this operator that works in a similar way to those found in [[C (programming language)|C]]:
<pre>
b +<< a # Equivalent to b << b + a
</
To print a variable you can use the following command
Line 47:
===Symbols===
As well as conventional data types such as integers, floats and strings Magik also implements symbols. Symbols are a special token data type that are used extensively throughout Magik to uniquely identify objects. They are represented by a colon followed by a string of characters. Symbols can be escaped using the [[vertical bar]] character. For example:
<pre>
a << :hello # whenever :hello is encountered, it is the same instance
b << :|hello world|
</
===Dynamic typing===
Magik variables are not typed as they are in say [[C Sharp (programming language)|C#]] and can reference different objects at runtime. Everything in Magik is an object (there is no distinction between objects and primitive types such as integers):
<pre>
a << 1.2 # a floating point number is assigned to variable 'a'
a << "1.2" # later, a string is assigned to variable 'a'
</
;Objects
Objects are implemented in Magik using exemplars. Exemplars have similarities to classes in other programming languages such as [[Java (programming language)|Java]], but with important differences. Magik supports multiple inheritance, and [[mixin]]s (which implement functionality with no data). New instances are made by cloning an existing instance (which will typically be the exemplar but does not have to be).
New exemplars are created using the statement <code>def_slotted_exemplar()</code>, for example:
<pre>
def_slotted_exemplar(:my_object,
{
Line 67:
{:slot_b, "hello"}
}, {:parent_object_a, :parent_object_b})
</
This code fragment will define a new exemplar called <code>my_object</code> that has two slots (or fields) called <code>slot_a</code> (pre-initialized to 34) and <code>slot_b</code> (pre-initialised to "hello") that inherits from two existing exemplars called <code>parent_object_a</code> and <code>parent_object_b</code>.
Line 74:
For example:
<pre>
a << "hello"
b << "hello"
Line 84:
b << a = b # returns True (_true) because the values of a and b are equal
a _is b # returns True (_true) because b was assigned the specific instance of the same object as a, rather than the value of a.
</
===Methods===
Methods are defined on exemplars using the statements <code>_method</code> and <code>_endmethod</code>:
<pre>
_method my_object.my_method(a, b)
_return a + b
_endmethod
</
It is convention to supply two methods <code>new()</code> (to create a new instance) and <code>init()</code> (to initialize an instance).
<pre>
# New method
_method person.new(name, age)
Line 108:
_return _self
_endmethod
</
The <code>_clone</code> creates a physical copy of the <code>person</code> object. The <code>_super</code> statement allows objects to invoke an implementation of a method on the parent exemplar. Objects can reference themselves using the <code>_self</code> statement. An object's slots are accessed and assigned using a dot notation.
Line 114:
Optional arguments can be declared using the <code>_optional</code> statement. Optional arguments that are not passed are assigned by Magik to the special object <code>_unset</code> (the equivalent of null). The <code>_gather</code> statement can be used to declare a list of optional arguments.
<pre>
_method my_object.my_method(_gather values)
_endmethod
</
===Iteration===
In Magik the <code>_for</code>, <code>_over</code>, <code>_loop</code> and <code>_endloop</code> statements allow iteration.
<pre>
_method my_object.my_method(_gather values)
total << 0.0
Line 132:
m << my_object.new()
x << m.my_method(1.0, 2, 3.0, 4) # x = 10.0
</
Here values.elements() is an iterator which helps to iterate the values.
In Magik [[Generator (computer science)|generator]] methods are called iterator methods. New iterator methods can be defined using the <code>_iter</code> and <code>_loopbody</code> statements:
<pre>
_iter _method my_object.even_elements()
_for a _over _self.elements()
Line 146:
_endloop
_endmethod
</
===Procedures===
Magik also supports functions called procedures. Procedures are also objects and are declared using the <code>_proc</code> and <code>_endproc</code> statements. Procedures are assigned to variables which may then be invoked:
<pre>
my_procedure << _proc @my_procedure(a, b, c)
_return a + b + c
Line 155:
x << my_procedure(1, 2, 3) # x = 6
</
===Language Quirks===
Because Magik was originally developed in England, methods in the core smallworld libraries are spelled using [[British English]]. For example:
|