HOME

TheInfoList



OR:

Magik is an
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
language that supports
multiple inheritance Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
and polymorphism, and it is
dynamically typed In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
. 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 GE Power (formerly known as GE Energy) is an American energy industry, energy technology company, owned by General Electric. Structure As of July 2019, GE Power is divided into the following divisions: * GE Gas Power (formerly Alstom Power, Als ...
, 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 A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
. The successful porting was confirmed by
Oracle Corporation Oracle Corporation is an American multinational computer technology corporation headquartered in Austin, Texas. In 2020, Oracle was the third-largest software company in the world by revenue and market capitalization. The company sells da ...
in November of the same year.


Similarities with Smalltalk

Magik itself shares some similarities with
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
in terms of its language features and its architecture: the Magik language is compiled into byte codes interpreted by the Magik
virtual machine In computing, a virtual machine (VM) is the virtualization/emulation of a computer system. Virtual machines are based on computer architectures and provide functionality of a physical computer. Their implementations may involve specialized hardw ...
. The Magik virtual machine is available on several platforms including
Microsoft Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
, various flavours of
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
and
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
. Magik is console based and code can be modified on the fly even when an application is running. The console can also be used to execute Magik code and to see the results. 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

Magik uses the # token to mark sections of code as comments: # This is a comment.


Assignments

Magik uses the << operator to make assignments:
  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 Comparison or comparing is the act of evaluating two or more things by determining the relevant, comparable characteristics of each thing, and then determining which characteristics of each are similar to the other, which are different, and t ...
. Magik also supports a compressed variation of this operator that works in a similar way to those found in C:
  b +<< a # Equivalent to b << b + a
To print a variable you can use the following command 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 The vertical bar, , is a glyph with various uses in mathematics, computing, and typography. It has many names, often related to particular meanings: Sheffer stroke (in logic), pipe, bar, or (literally the word "or"), vbar, and others. Usage ...
character. For example:
  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# 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):
  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 Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
, but with important differences. Magik supports multiple inheritance, and
mixin In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depend ...
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 def_slotted_exemplar(), for example:
  def_slotted_exemplar(:my_object,
  , )
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 = 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.


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
  _method person.new(name, age)
    _return _clone.init(name, age)
  _endmethod

  # Initialise method.
  _private _method person.init(name, age)
     # Call the parent implementation.
     _super.init(name, age)
     # Initialise the slots.
     .name << name
     .age << 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.
  _method my_object.my_method(_gather values)     
  _endmethod


Iteration

In Magik the _while, _for, _over, _loop and _endloop statements allow iteration.
_block
	_local s << 0 
	_local i << 0
	_while i <= 100
	_loop 
		s +<< i 
		i +<< 1 
	_endloop
>> s
_endblock
Here, the _while is combined with _loop and _endloop.
  _method my_object.my_method(_gather values)
    total << 0.0
    _for a _over values.elements()
    _loop
       total +<< a
    _endloop
    _return total
  _endmethod

  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 Generator may refer to: * Signal generator, electronic devices that generate repeating or non-repeating electronic signals * Electric generator, a device that converts mechanical energy to electrical energy. * Generator (circuit theory), an eleme ...
methods are called iterator methods. New iterator methods can be defined using the _iter and _loopbody statements:
  _iter _method my_object.even_elements()
    _for a _over _self.elements()
    _loop
      _if a.even? _is _true
      _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:
  my_procedure << _proc @my_procedure(a, b, c)
    _return a + b + c
  _endproc

  x << my_procedure(1, 2, 3) # x = 6


Regular Expression

Magik supports // regular expression syntax:
_if /Hello\,\s(\w)+!/.matches?("Hello, Magik!") _then
    write("Got a match!")
_endif 
and to capture groups in Regex:
/sw( -9)-( -9).*/.replace_all("sw65456-324sss", "$1") # "65456"
/sw( -9)-( -9).*/.replace_all("sw65456-324sss", "$2") # "324"


HTTP Library

Magik supports making HTTP or HTTPS requests via http library, see below examples:
magikhttp << http.new()
magikhttp.url("https://www.google.com").get()
magikhttp.url("https://www.google.com").post(, "some data")


Language Quirks

Because Magik was originally developed in England, methods in the core smallworld libraries are spelled using
British English British English (BrE, en-GB, or BE) is, according to Lexico, Oxford Dictionaries, "English language, English as used in Great Britain, as distinct from that used elsewhere". More narrowly, it can refer specifically to the English language in ...
. 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


Hello World example

The following is an example of the
Hello world program ''Hello'' is a salutation or greeting in the English language. It is first attested in writing from 1826. Early uses ''Hello'', with that spelling, was used in publications in the U.S. as early as the 18 October 1826 edition of the ''Norwich C ...
written in Magik: write("Hello World!")


References


External links


Smallworld Product Suite TechnologyMDT - Magik Development Tools
IDE for GE Smallworld GIS developers
Open Source (SourceForge)Technical Paper No. 5 - An Overview of Smallworld MagikGE Smallworld, Emacs Extensions for Magik developersVisual Studio Code extension for Smallworld Magik programming language.
{{DEFAULTSORT:Magik Class-based programming languages Dynamically typed programming languages