Previous ... Next

Basic classes

(1/5) Num

«» is a general number type, implemented as an arbitrary precision rational. When in doubt of what numeric type to use, «» is a good first guess. Some examples of usage:

Another useful numeric type is «», for index and index offsets. It corresponds to sizes and indexes in sequences. «»s are returned by «» methods and are expected as parameter by indexing methods. «» represent 32 bit integers with the usual but tricky modulo arithmetic.

Other numeric types

AdamsTowel offers two other numeric types: «» (64 bits floating point) and «» (64 bits integers, rarely used).


Conversions between various numeric classes must be performed explicitly. AdamsTowel offers a simple way to convert between numeric classes; all numeric classes implements «» so that they can be converted in to each other using the empty named method. For example we can convert indexes into doubles by writing «». This will avoid precision loss as much as possible.

(2/5) Units: An example library

We will now see how to load and use an interesting 42 Library:«». Consider the following code, where the class decorator «» allows us to load libraries and embed them in the current context, while the «» keyword imports the code from the web.

The library «» offers methods to create units out of numeric supports, like «» and «». The code above shows how to create a «» unit and use it to represent a person age. Units can be added to themselves and multiplied by constants; for example «» and «» would hold, but «» would not compile. Units could be used to manually define all of the units of the SI system. Prebuilt reusable code for the SI system is already provided in the library; we simply need to specify the desired support, as shown in the code below:
res = (6SI.Meter + 4SI.Meter) * 2Num //20Meter
//wrong = 6SI.Meter + 2SI.Second
«» is a trait; traits contains reusable code and operations to adapt it to the current needs. We will see more on traits (much) later in this guide.
In the case of «», we can adapt it to many kinds of numeric support and extract the code using «Num]]]>». The syntax «Num]]]>» maps the class called «» inside of the library onto the class «» defined outside of the library. We will explain the precise use of such mappings later.

As you can see, we can sum meters together, and we can use the support for multiplication, but we can not mix different units of measure. Mathematically you can obtain the support out of the unit by division; that is, 42 meters divided by 2 meters is 21. Units also provide method «», which is just extracting the value of the support from the unit. This can be convenient during programming but does not make a lot of sense mathematically and thus it should be used with care, similarly to other methods starting with «».

Some code which uses units in interesting ways:

0SI.Newton (Debug(S"I can fly"))
myAcc = myLift/stackWeight
reachedHeight = (myAcc*t*t)/2Num //after t (10 sec)
//works assuming the rocket fuel burnt in 10 sec is negligible

(3/5) Alphanumeric

In the same way «» allows easy creation of arithmetic classes, «» allows easy creation of alphanumeric classes: classes that can be instantiated from a string literal that follow certain properties.

Note: we raise an error if «» does not have the shape we expected. We will see errors/exception in more detail soon. We can define fields, and compute their values by parsing the string. It is common to propagate the original string from the factory into the object; but it is not mandatory. For example you could apply some form of normalization, as shown below:

(4/5) Enumerations

Enumerations can be obtained with «», as in the following code:

Enumerations allows us to add operations as methods on the cases, as shown below:

(5/5) Summary

  • We had a look at the most basic features of AdamsTowel. There is rich support for defining your own specialized data structures instead of having to rely on the ones provided by default.
  • Use «», «» and «» to give meaning to your constants. In this way, the type system will help you to use values with the semantics that you decided. Be sure to define all of the right base classes to establish a convenient vocabulary to talk about your problem domain.

      Previous ... Next