Previous ... Next

Target audience

This tutorial is design for expert programmers, already knowledgeable in at least two or three of the languages Java, C#, C++ and Python. This tutorial lays out the basic knowledge for programming in 42 using AdamsTowel, but does not explore the foundational theory behind 42, or the mathematical rationale for the correctness of 42.
The language 42 and many 42 metaphors are inspired by The Hitchhiker's Guide to the Galaxy by Douglas Adams.

Downloading and running

Currently, you can download and run 42 as a Java program.

Basics

(1/5)Simple hello world program

Let's look at a simple hello world program:


When we write «» we are asking 42 to reuse the code of the library found in the internet address «». AdamsTowel is our towel, that is the set of classes and interfaces that we wish to start from. A towel usually plays the role of "the standard library" of most languages. «» is the main website of 42, where most commonly used libraries are hosted. To reuse code you need an internet connection; but this also means that you will never have to manually import any code. Required code will be downloaded and cached on your machine, so you need not to be aware of the existence of this mechanism.

We do not need to always start from AdamsTowel; there are many interesting towels out there, and you may also become skilled in the advanced technique of towel embroidery. In this tutorial, all of our examples are expressed reusing «».

At the right of «» we write the expression that we wish to execute; in this case we just print out using the «» class. «» is not a method, and «» is not special name either. You can replace it with «» or any other valid upper-case name. In 42 there is no concept of main method as in Java or C. For now you can think of «» as a top level command. We will understand later how this fits with the general language design.

«» is a simple class whose most important method print a message on the terminal.

In 42, when a class has a most important method, it is conventional to use the empty name, so that can be used with the short syntax «» instead of a more verbose «».
In 42, Strings and numbers need to be created using their type, as in «» or «». Indeed «» is just a convenient syntax equivalent to «»; the syntax with quotes is needed to express negative or fractional number literals, as for example «» or «».

(2/5)Method declaration and call

Let's now define a method and call it.


Here we define a class to host our «» method. We write «» to define a method that can be called on the class object, as in «». This is roughly equivalent to a static method in languages like Java or C++ , or class methods in Python. Note that «» inserts a value into a string.

Note that the method is called using the parameter name explicitly. We believe this increases readability.

You may also notice how there are two different usages for curly brackets: if there is at least one «» keyword then the expression is a block of statements, otherwise the expression is a library literal, which can contains methods and nested libraries. A nested library is denoted by an upper-case name, and can be created from a library literal or from an expression producing a library literal. A library literal can be a class (default case) or an interface (starts with the «» keyword). A nested library in 42 is similar to a static inner class in Java, or a nested class in C++. It is just a convenient way to separate the various components of our program and organize them into a tree shape.

The class «» from before offers a single class method, has no fields and you can not create instances of «», since no factory is present. In 42 we do not have constructors. Objects are created by factory methods, that are just normal methods that happen to return an instance of their class. We believe this is a much simpler and more consistent approach to object initialization than having special syntax that encourages programmers to make assumptions about the behaviour of the operations.

(3/5)Simple class with internal state

Now we show a class with state and a factory method:


Here you can see we define a «» class with coordinates «» and «» of type «», unlimited precision rational number. In addition to «», «», «» and «», «» will offer many other useful methods, since it has been declared using «».

Indeed, «» is a decorator. Decorators are classes/objects that offer an operator «», called the decorator operator, whose goal is to translate a library into a better library. In this case, «» is translating the class «» into a much longer class, with a factory method taking in input the fields and initializing them; but also containing boring but useful definitions for equality, inequality, conversion to string and many others.

Finally, we define a methods to add to each of the coordinates. For very short methods we can omit the curly brackets and «». Indeed, method bodies are just expressions, and the curly brackets turn a block of statements into one expression. In the method «» we show how to create a new «» instance and how to call getter methods. In the method «» we show an improved version, using the «» method, another gift of Data, which allows us to easily create a clone with one or more fields updated. We can define two methods, «» and «» with the same method name, if parameter names are different.

Note that we always use getters and we never access fields directly. In many other languages we can use write «» and «». Such syntax does not exists in 42. The same goes for object instantiation; in many languages there is a dedicated «» syntax, while in 42 it is just a method call.

Also, similarly to what happens in Python, we need to use «» to call methods when the receiver is «». While it makes some code more verbose, naming the receiver avoids ambiguities about scoping and nesting for method resolution.

Decorators

Decorators, such as «», are one of the main concepts used by 42 programmers. We will encounter many decorators in this tutorial. For now, just get used to the pattern of writing «» to go from a minimal chunk of code, with method declarations for the application specific bits, to a fully fledged usable class.

The backslash «»

In 42, we can use the «» character as a shortcut. There are two different ways to use the backslash: as a keyword or immediately followed by a lowercase identifier.

As a keyword, «» represents the expected type of the surrounding expression. The slash searches outwards on super expressions until it finds a place with an easily guessable type: the return type of the method, a method parameter or a local binding with an explicit type. For example:

could be shortened as
Consider these other examples:

Followed by a method name (and method parameters if any) a «» represents the receiver of the innermost method invocation. Thus, for example

could be shortened as
Consider this other example:

In the rest of the tutorial, we will use «» when it saves space. This shortcut seems unusual at first, but with a little of experience becomes very clear. 42 is a pure OO language, where the method call is the central operation. This syntax allows for the expressions of the method parameters to depend on the method receiver. We will see that this enables many interesting micropatterns.

(4/5)Collection.list

Lists can be defined using «», as in the example below,

where we define new classes «» and «». Note that those are new classes in a nominal type system, so in
«» and «» denote different classes, with different types. As you can see, lists can be initialized with «». In this case, this syntax is equivalent to creating a new empty list and then calling the «» method one time for each of the expressions separated by «». Of course, the parameter type of that method is the element type of the list, so «» finds it as an easily guessable type.

Consider now the following code:

As you can see, we can use «» to iterate on multiple collections at once.
In 42 as in most other languages you can have blocks of code where multiple local bindings are introduced by associating a lowercase name with an initialization expression. Similarly, the «» introduces local bindings whose values will range over collection elements by associating them with initialization expressions for iterators.

(5/5)First summary

  • At the start of your program, import a towel using «», as in «».
  • To define a simple class exposing its state and some methods working with those, use «», as in «».
  • You can define methods in classes with the «» keyword. Use «» for methods that can be called on the class object directly.
  • To introduce the concept of list for a certain type, use «»
    as in the class declaration «»

Object creation summary

42 supports many different syntactic forms that are convenient for creating objects:

  • 12Num: from a numeric representation
  • S"foo": from a string representation
  • Point(x=_,y=_): from the parameter values
  • Points[_;_;_]: from a variable length sequence of values.
Note that in 42 these are all just expressions, and represent one or more methods in the named class. This means that even concepts quite different from numbers, strings and collections may benefit from this syntactic support.

Digressions / Expansions

Here, after the summaries, we will digress and expand on topics related to the chapter. Digressions/expansions may be more technical and challenging, and may refer to any content in any of the other chapters, including the forward ones. For example, we are now going to make some more precise remarks about:

Method selector

In 42 a method selector is the method name plus the list of all the parameter names, in order. Methods in a class must be uniquely identified by their method selectors. This provides a good part of the expressive power of overloading, while avoiding all the complexities of type driven overloading resolution.

      Previous ... Next