Content deleted Content added
Hervegirod (talk | contribs) No edit summary |
m →External links: HTTP to HTTPS for SourceForge |
||
(39 intermediate revisions by 27 users not shown) | |||
Line 1:
{{Short description|Object-oriented programming language}}
{{primary sources|date=January 2008}}
'''Magik''' is an [[object-oriented programming]] language that supports [[multiple inheritance]]
Magik (Inspirational Magik) was originally introduced in 1990 and has been improved and updated over the years. Its current version is
In July 2012, Magik developers announced that they were in the process of porting Magik language on the [[Java virtual machine]]. The successful porting was confirmed by [[Oracle Corporation]] in November of the same year.<ref>{{cite web
| url=https://blogs.oracle.com/jtc/entry/sprinkle_some_magik_on_that
| title=Sprinkle Some Magik on that Java Virtual Machine
Line 17 ⟶ 12:
| author=Jim Connors
| quote=''With this new capability GE Energy has succeeded in hosting their Magik environment on top of the Java Virtual Machine''
| accessdate=2012-11-06}}</ref>
==Similarities with Smalltalk==
Line 28 ⟶ 23:
==Language features==
Magik uses the <code>#</code> token to mark sections of code as comments:
# This is a comment.
Magik uses the <code><<</code> operator to make [[Assignment (computer science)|assignments]]:
<pre>
a << 1.234
b << b + a
c << "foo" + "bar" # Concat strings
</pre>
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
</pre>
To print a variable you can use the following command
Line 51 ⟶ 46:
write(a)
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|
</pre>
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'
</pre>
;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 73 ⟶ 68:
{:slot_b, "hello"}
}, {:parent_object_a, :parent_object_b})
</pre>
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-initialised 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>.
===Comparison===
Magik implements all usual logical operators (<code>=</code>, <code><</code>, <code><=</code>, <code>></code>, <code>>=</code>, <code>~=/<></code>) for comparison, as well as a few unusual ones. The <code>_is</code> and <code>_isnt</code> operators are used for comparing specific instances of objects, or object references rather than values.
For example:
<pre>
a << "hello"
b << "hello"
a = b # returns True (_true) because the values of a and b are equal
a _is b # returns False (_false) because a is not the same instance as b
a << "hello"
b << a
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.
</pre>
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
</pre>
It is convention to supply two methods <code>new()</code> (to create a new instance) and <code>init()</code> (to
<pre>
# New method
_method person.new(name, age)
Line 105 ⟶ 102:
_endmethod
#
_private _method person.init(name, age)
# Call the parent implementation.
Line 114 ⟶ 111:
_return _self
_endmethod
</pre>
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 120 ⟶ 117:
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
</pre>
===Iteration===
In Magik the <code>_while</code>, <code>_for</code>, <code>_over</code>, <code>_loop</code> and <code>_endloop</code> statements allow iteration.
<pre>
_block
_local s << 0
_local i << 0
_while i <= 100
_loop
s +<< i
i +<< 1
_endloop
>> s
_endblock
</pre>
Here, the _while is combined with _loop and _endloop.
<pre>
_method my_object.my_method(_gather values)
total << 0.0
Line 138 ⟶ 151:
m << my_object.new()
x << m.my_method(1.0, 2, 3.0, 4) # x = 10.0
</pre>
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 152 ⟶ 165:
_endloop
_endmethod
</pre>
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 161 ⟶ 175:
x << my_procedure(1, 2, 3) # x = 6
</pre>
===Regular expression===
Magik supports // regular expression syntax:
<pre>
_if /Hello\,\s(\w)+!/.matches?("Hello, Magik!") _then
write("Got a match!")
_endif
</pre>
and to capture groups in Regex:
<pre>
/sw([0-9]+)-([0-9]+).*/.replace_all("sw65456-324sss", "$1") # "65456"
/sw([0-9]+)-([0-9]+).*/.replace_all("sw65456-324sss", "$2") # "324"
</pre>
===HTTP library===
Magik supports making HTTP or HTTPS requests via http library, see below examples:
<pre>
magikhttp << http.new()
magikhttp.url("https://www.google.com").get()
magikhttp.url("https://www.google.com").post({"User-agent", "Bot"}, "some data")
</pre>
===Language quirks===
Because Magik was originally developed in England, methods in the core smallworld libraries are spelled using [[British English]]. For example:
Use "initialise", not "initialize".
Like other programming language Magik too has collections. They include the following:
*Simple Vector
*Rope - a [[dynamic array]]
*[[Hash Table]]
*Property List
*Equality set
Line 179 ⟶ 219:
The following is an example of the [[Hello world program]] written in Magik:
write
==References==
{{Reflist}}
==External links==
*[http://www.ge-energy.com/products_and_services/products/geospatial_systems_and_mobile_workforce/index.jsp Smallworld Product Suite Technology]
*[http://www.mdt.net/ MDT - Magik Development Tools] IDE for GE Smallworld GIS developers
*[
*[http://lambda-the-ultimate.org/classic/message5839.html#5886 Language forum post on Magik]
*[https://archive.today/20061020015202/http://cfis.savagexi.com/pages/technical_paper_5 Technical Paper No. 5 - An Overview of Smallworld Magik]
*[http://www.magikemacs.com/page/6/ GE Smallworld, Emacs Extensions for Magik developers] {{Webarchive|url=https://web.archive.org/web/20160425011048/http://www.magikemacs.com/page/6/ |date=2016-04-25 }}
*[https://marketplace.visualstudio.com/items?itemName=siamz.smallworld-magik Visual Studio Code extension for Smallworld Magik programming language.]
{{DEFAULTSORT:Magik}}
[[Category:Class-based programming languages]]
[[Category:Dynamically typed programming languages]]
|