Magik (Programming Language)
Magik (Programming Language)
Magik is an object-oriented programming language that supports multiple inheritance and polymorphism,
and it is dynamically typed. It was designed and implemented in 1989 by Arthur Chance of Smallworld
Systems Ltd. as part of Smallworld Geographical Information System (GIS). Following Smallworld's
acquisition in 2000, Magik is now provided by GE Energy, still as part of its Smallworld technology
platform.
Magik (Inspirational Magik) was originally introduced in 1990 and has been improved and updated over
the years. Its current version is 5.2.
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.[1]
Contents
Similarities with Smalltalk
Language features
Comments
Assignments
Symbols
Dynamic typing
Comparison
Methods
Iteration
Procedures
Regular Expression
HTTP Library
Language Quirks
Collections
Hello World example
References
External links
Compiled code is stored in a single file called an image file. Each image file holds the compiled byte-codes
and the state of the session (for example variable values) when the image was last saved.
Language features
Comments
# This is a comment.
Assignments
a << 1.234
b << b + a
For clarity, this notation is read as "a becomes 1.234" or "b becomes b plus a". This terminology separates
assignment from comparison.
Magik also supports a compressed variation of this operator that works in a similar way to those found in C:
a << "hello"
write(a)
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:
b << :|hello world|
Dynamic typing
Magik variables are not typed as they are in say 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):
Objects
Objects are implemented in Magik using exemplars. Exemplars have similarities to classes in other
programming languages such as Java, but with important differences. Magik supports multiple inheritance,
and mixins (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 def_slotted_exemplar(), for example:
def_slotted_exemplar(:my_object,
{:slot_a, 34},
{:slot_b, "hello"}
}, {:parent_object_a, :parent_object_b})
This code fragment will define a new exemplar called my_object that has two slots (or fields) called
slot_a (pre-initialised to 34) and slot_b (pre-initialised to "hello") that inherits from two existing
exemplars called parent_object_a and parent_object_b.
Comparison
Magik implements all usual logical operators (=, <, <=, >, >=, ~=/<>) for comparison, as well as a few
unusual ones. The _is and _isnt operators are used for comparing specific instances of objects, or
object references rather than values.
For example:
a << "hello"
b << "hello"
a << "hello"
b << a
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 _method and _endmethod:
_method my_object.my_method(a, b)
_return a + b
_endmethod
It is convention to supply two methods new() (to create a new instance) and init() (to initialise an
instance).
# New method
_endmethod
# Initialise method.
_super.init(name, age)
_return _self
_endmethod
The _clone creates a physical copy of the person object. The _super statement allows objects to
invoke an implementation of a method on the parent exemplar. Objects can reference themselves using the
_self statement. An object's slots are accessed and assigned using a dot notation.
Methods that are not part of the public interface of the object can be marked private using the _private
statement. Private methods can only be called by _self, _super and _clone.
Optional arguments can be declared using the _optional statement. Optional arguments that are not
passed are assigned by Magik to the special object _unset (the equivalent of null). The _gather
statement can be used to declare a list of optional arguments.
_endmethod
Iteration
In Magik the _while, _for, _over, _loop and _endloop statements allow iteration.
_block
_local s << 0
_local i << 0
_loop
s +<< i
i +<< 1
_endloop
>> s
_endblock
_loop
total +<< a
_endloop
_return total
_endmethod
m << my_object.new()
In Magik generator methods are called iterator methods. New iterator methods can be defined using the
_iter and _loopbody statements:
_loop
_then
_loopbody(a)
_endif
_endloop
_endmethod
Procedures
Magik also supports functions called procedures. Procedures are also objects and are declared using the
_proc and _endproc statements. Procedures are assigned to variables which may then be invoked:
_return a + b + c
_endproc
x << my_procedure(1, 2, 3) # x = 6
Regular Expression
write("Got a match!")
_endif
HTTP Library
Magik supports making HTTP or HTTPS requests via http library, see below examples:
magikhttp.url("https://www.google.com").get()
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".
Collections
Like other programming language Magik too has collections. They include the following:
Simple Vector
Rope
Hash Table
Property List
Equality set
Bags
write("Hello World!")
References
1. Jim Connors (2012-11-05). "Sprinkle Some Magik on that Java Virtual Machine" (https://blog
s.oracle.com/jtc/entry/sprinkle_some_magik_on_that). Retrieved 2012-11-06. "With this new
capability GE Energy has succeeded in hosting their Magik environment on top of the Java
Virtual Machine"
External links
Smallworld Product Suite Technology (http://www.ge-energy.com/products_and_services/pr
oducts/geospatial_systems_and_mobile_workforce/index.jsp)
MDT - Magik Development Tools (http://www.mdt.net/) IDE for GE Smallworld GIS
developers
Open Source (SourceForge) (http://sourceforge.net/projects/magikcmpnts)
Language forum post on Magik (http://lambda-the-ultimate.org/classic/message5839.html#5
886)
Technical Paper No. 5 - An Overview of Smallworld Magik (https://archive.today/200610200
15202/http://cfis.savagexi.com/pages/technical_paper_5)
GE Smallworld, Emacs Extensions for Magik developers (http://www.magikemacs.com/pag
e/6/)
Visual Studio Code extension for Smallworld Magik programming language. (https://marketp
lace.visualstudio.com/items?itemName=siamz.smallworld-magik)