ArticleslgStudy

science

Magik (programming language)

Magik (programming language) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Magik (programming language) rather than just read about it. In short: 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).

Key takeaways

  • Magik (programming language) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Magik (programming language) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Magik (programming language) from memory before moving on to harder problems.

Reference excerpt

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.

Similarities with Smalltalk Magik itself shares some similarities with Smalltalk in terms of its language features and its architecture: the Magik language is compiled into byte codes interpreted by the Magik virtual machine. The Magik virtual machine is available on several platforms including Microsoft Windows, various flavours of Unix and Linux. 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. 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 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, 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 = 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 methods are called iterator methods. New iterator methods can be defined using the _iter and _loopbody statements:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Magik (programming language)

Start with the simplest possible case. Write down what Magik (programming language) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Magik (programming language) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Magik (programming language) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Magik (programming language)

In research
Magik (programming language) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Magik (programming language) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Magik (programming language) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Class-based programming languages, Dynamically typed programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Magik (programming language) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Magik (programming language)” →

How to study Magik (programming language) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Magik (programming language) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Magik (programming language) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Magik (programming language) in simple terms?

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).

Why does Magik (programming language) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Magik (programming language)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Magik (programming language).

Tags

  • Class-based programming languages
  • Dynamically typed programming languages

Keep exploring