Chapter 1
Introduction
It is often thought that the languages commonly and widely used by programmers
are perfect. As far the term perfect in general can not be used properly, it repre-
sents the feeling, in my opinion di used, of maximum con dence about reliability and
completeness of the language itself and thus of the development platform.
Another important issue which should be considered concerns the rules of modern
market that impose and require to the developers a growing organization aimed at
producing software rich in content, with few defects and quickly.
Which one can be a response from the computer science research? We think that
it can be methods, models and languages that press forward to the maximum software
composability, safety and e ciency, in order to promote the distribution of the software
development in di erent teams as well as the reuse of pieces of code previously written.
As already mentioned, people place great con dence in the model of object-oriented
programming, largely used in languages such as C++[25] and Java [28], and often ignore
which may be their weaknesses, or worse, potential hidden problems. In particular,
in this thesis, we look at some of the problems closely linked to the mechanisms of
inheritance, with the aim of improving composability and thus facilitate code reuse.
We address the issue by proposing a new language, named Magda, which we use
as model to describe some of the problems and to o er possible solutions. The study
and design of the Magda language, in terms of motivations, structures, metatheory,
was presented in Jarek Ku smierek’s PhD thesis [2] which led also to a rst evaluable
prototype. We build then on that work. Magda is proposed as an alternative language
that o ers possible solutions to:
Limitations inherent to single inheritance and problems connected to multiple
inheritance.
Lack of modularity of initiation protocol (contructors).
Accidental name clashes on method signatures and eld names.
More precisely, this new language does not exploit the class-based paradigm of
traditional object oriented programming, but it is based on the notion of mixin [7, 6,
8, 9, 10, 11, 12]. We will return on this notion in the sequel.
My task was to test thoroughly a prototypical implementation of Magda, previously
developed, via programming applications useful to highlight the innovative features of
the language. As an important side e ect, this work led to a tuning of the prototype
to make it more usable and complete, as well as the rede nition of the formal syntax
of the language, with respect to the implementation.
5
An entire chapter (Chapter 3) is dedicated to the description of the practical imple-
mentation of the prototype, of its use and its structure and a further chapter (Chap-
ter 4) examines, one by one, the more signi cant application examples that were devel-
oped. Each application is presented with its own introduction and motivation, followed
by a source code description and the execution output. The modularity is demonstrated
also through the implementation of some signi cant design patterns (see [24]). At the
end of the document there is a discussion of the results obtained, which may lead to
new developments of Magda.
A proof-of-concept implementation of Magda, with examples and a how-to, is avail-
able at:
http://sourceforge.net/projects/magdalanguage
The description of this Magda package prototype is presented in Chapter 3. The dis-
cussed prototype and all other related pratical aspects of this thesis refer to the package
version built on 17/03/2013, SVN commit [r9].
6
Chapter 2
The Magda language
In this chapter, we introduce the motivations behind Magda, and the main features
of the language will be presented through some examples, with particular emphasis
on the composition mechanism and on the initialization design (which is the new and
novel part of the language proposal). All the details can be found in [2].
The limitations of mainstream object-oriented languages which have been the mo-
tivations for the design of this new language, where it aims to show a possible solution,
are the following.
Limitations of composition mechanisms. The most widely applied composition
and reuse mechanism is inheritance. In its classical, single-inheritance version,
this mechanism is simple, however often not su cient. In order to overcome its
limits, other proposals were developed, like multiple inheritance, mixins [7, 6, 8, 9,
10, 11, 12] and traits [13, 14]. Multiple inheritance is perceived as too complicated
and too dangerous [15]. The two other constructs (mixins and traits) have been
designed to tame multiple inheritance and managed to improve reusability in
many aspects, however, they su er from problems related to name clashes.
Non-modular initialization protocol. In most object-oriented languages, the ini-
tialization protocols are implemented as constructors. Constructors are respon-
sible for the initialization of properties coming from di erent places in the class
hierarchy, yet they are monolithic, which means that they have to perform all the
job in one block of code. While a constructor can call a superclass constructor
to delegate part of the work, it still needs to keep all the superclass construc-
tor’s parameters in its own signature. This increases the amount of work which
must be performed when the initialization protocol needs to be modi ed. More-
over, assumptions about the superclass constructors are imposed, making those
constructors more di cult to change. Additionally, by creating unnecessary de-
pendencies, it limits the possible combinations of otherwise independent parts of
the code.
Accidental name clashes. Classes can be implemented from di erent components
(in standard languages, by extending other classes, or by implementing some in-
terfaces); it may then occur that the same method name has di erent meanings in
di erent components. This causes problems, from non-compilation to unexpected
behavior during the program execution.
7
2.1 Overview
The language Magda is built around the notion of mixin, which de nes the building
block from which objects are created. More precisely a mixin is a class parameterized
over a superclass, introduced to model some forms of multiple inheritance and improve
code modularization and reusability [7, 6, 8, 9, 10, 11, 12, 16, 17, 18].
There are usually two operations de ned on mixins: ( i) application, by which a
mixin is applied to a class to obtain a fully- edged subclass (the class argument plays
the role of the parent); (ii) composition, which makes a more specialized mixin by
composing two existing ones. Notice that an indirect form of composition is possible
even in the presence of application only, by applying a chain of mixins to a class (which
is the way a linearized multiple inheritance is obtained). A mixin can defer de nition
and binding of methods until runtime, though attributes and instantiation parameters
are still de ned at compile time. This approach di ers from the class-based one and
makes available a more powerful way to reusing code.
In Magda there is no concept of a class, therefore mixins are not interpreted as
functions from classes to classes. Mixins are used directly to create new objects from,
and also induce types in the nominal static type system of the language (as proposed
independently in McJava [19]). Additionally, to take advantage of mixin reusability
and enhance it, Magda contains two unique features. The most innovative one is the
modularization of constructors, described in the Section 2.3, in such a way the part
of the state initialization related to a feature is declared together with that feature.
Then, mixins with independent initialization protocols can be combined without the
need to copy any code. This feature was presented as a part of a Java extension called
JavaMIP [20].
The second distinctive feature of Magda is the way of how declarations of new
methods, overriding of existing methods, and method calls are speci ed, by declaring
and referencing identi ers in a univocal way. This results in the absence of name
clashes and accidental overridings. A version of this feature is the base of the HygJava
language, presented in [21].
2.2 Sintax and notation overview
Every program in Magda consists of two parts: a list of mixin declarations and a list
of instructions, called main instructions. The main instructions may use the mixin
declarations. Any type in Magda is a sequence of mixin names. Values are either null
or objects. Value null is the default value for variables and elds.
Magda instructions include:
the creation of a new object. Each object in Magda is created from a non-empty
sequence of mixins. The sequence of mixins plays a role similar to the one of a
class in other languages.
the method call. In Magda, as in most object-oriented languages, the set of
methods \understood" by an object depends on the \schema" from which it was
created. Therefore, the set of understood methods depends on the sequence of
mixins used to create the object.
In Figure 2.1 can be seen a simple and minimal program written in Magda that only
prints out the message string Hello World.
The program consists of the declaration of a mixin namedHelloWorld and one main
instruction. Mixin HelloWorld contains a method named MainMatter. MainMatter
8
mixin HelloWorld of Object =
new Object MainMatter()
begin
‘‘Hello world’’.String.print();
end;
end;
//
(new HelloWorld []).HelloWorld.MainMatter();
Figure 2.1: Minimal program example
begins with the keyword new which indicates the introduction of a new method identi-
er , as opposed to method rede nition , and abstract methods, both described later on.
The main instruction starts with the creation of an object from mixin HelloWorld,
using the new HelloWorld[] expression. Then it calls the method MainMatter on the
object, that, in turn, calls the method print, declared in mixin String. In Magda,
String is a built-in mixin and contains methods like print and add (which concate-
nates two strings). Similarly, Magda contains other built-in mixins like Integer and
Boolean. The Boolean mixin cannot be used in object creation expressions. Then, the
only way to obtain a Boolean value is to use one of the Boolean constants: true and
false. Since booleans are object values, null is also the default value for Boolean.
Moreover, Magda features two control instructions: a conditional instruction if and a
loop instruction while.
In each method call, the method name is pre xed with the name of the mixin in
which it was introduced. This is to enforce hygiene of identi ers in order to avoid
accidental clashes.
In the current version of Magda all methods are visible. This results in a similar
behavior as the one of public methods in Java.
The following sections will show on object usage, composability of inheritance de-
pendecies, abstract and virtual method declarations. About the initialization protocol
behaviour see the dedicated Section 2.3.2.
2.2.1 Object elds and local variables
All elds of an object are declared, with a name and a type, in the mixins from which
the object is created. The type of a eld is a sequence of mixin names. The value of
each eld can be either the null value or an object value, that is, a reference to an
object. For simplicity, we have chosen that each eld in a Magda object can be only
accessed by methods of that object, via an invocation of the form this.fieldId. As
a result we obtain a behavior close to the one of protected elds in other languages.
Furthermore, similarly to method identi ers, all elds are pre xed with the name of
the mixin in which the elds have been declared.
In the example in Figure 2.2 there is a declaration of a mixin Point2D containing
two eld declarations, x and y.
As in MainClass.MainMatter method, variable declarations are in the header of
the method, after the signature, before the keyword begin, The type of a variable is a
sequence of mixin names; in this example, variable p1 has type Point2D. Similarly to
elds, local variables can be assigned with object values.
9