There are some concepts in the design of the Elan language and Integrated Development Environment which don't belong with any particular keyword or language construct described in the Language Reference or any particular data type or functionality in the Library Reference.
This Concepts document is the place for those descriptions and explanations.
In most programming, the term variable describes a data entity in a program that contains a value and has a name by which you can read and write its value, i.e. its value can be varied. In an Elan program, these data entities are called named values so that a distinction can be made between:
This distinction applies both to single values and to data structures and, while it is most pertinent in functional programming, it provides a useful discipline in any code.
Elan's named values are statically typed: when initialised, their Type is established and cannot be changed.
The Types and their names as used in the language are here listed, linking to further detail:
| Mutable | Type name | notes | |
|---|---|---|---|
| Fundamental Types | |||
| Integer | ✘ | ||
| Floating point | ✘ | ||
| Boolean | ✘ | ||
| String | ✘ | ||
| Group of values | see note | ||
| Mutable data structures | |||
| Simple array | ✔ | ||
| 2-dimensional array | ✔ | ||
| List | ✔ | ||
| Look-up dictionary | ✔ | ||
| Immutable data structures | |||
| List | ✘ | ||
| Look-up dictionary | ✘ | ||
| Set (maths like) | ✘ | ||
| LIFO Stack | |||
| FIFO Queue | |||
| Data I/O classes | |||
| Text file input | |||
| Text file output | |||
| Graphical | |||
| Turtle | |||
| Vector graphic | |||
| Other | |||
| Random number | |||
| Function reference | |||
| User defined | |||
| Application Class | (name of Class) |
| define named value | named value | notes | ||||
|---|---|---|---|---|---|---|
| with | as mutable Type | as immutable Type | re-assign with ' | mutate | scope | |
| ✘ | ✔ | ✘ | ✘ | global | ||
| ✔ | ✘ | ✔ | local | |||
| ✔ | ✘ | ✘ | local | |||
| ✔ | ✔ | ✔ | ✔ | local | ||
| parameter | ✔ | ✘ | ✔ | local | formal input argument of function, procedure or | |
| parameter | ✔ | ✘ | ✘ | local | formal input argument of function, procedure or | |
| ✔ | ✔ | ✔ | local | formal output argument of procedure | ||
| ✔ if actual argument defined with | ✔ | ✔ | local | formal output argument of procedure | ||
Names are given to all
You use
You can then use
You use
Immutables are useful because they can:
For immutable values that are of use throughout your program, you can use
It is good practice to make as many of your named values as possible immutable, either by defining them as
The Elan language has been carefully designed so that it supports static typing without the need for beginners to include the names of types in their programs. For example you can write a program that counts to 10 without using the type name "Int".
But there comes a point when you need to refer to the names of types when defining:
The names of all the built-in types are specified in the Library Reference. You can create your own types as classes, records and enums.
We use the word "method" to describe functions and procedures in general.
There are many functions and procedures which belong to system-defined types or user-defined classes, and operate on an instance of that type or class.
For example the function
In this case, the variable
A procedure may be invoked like this:
The variable
Functions and procedures that are defined as part of a user-defined class are also invoked from outside the class using a dot. For example a function:
and a procedure:
Collectively, these are known as dot methods. The functions and procedures that you define at a global level (ie not in a class or record) are not dot methods. There are also functions (eg abs) and procedures (eg clearPrintedText) provided by the Elan Library which are standalone. That is, they are not dot methods, and are used without a dot.
Some data structures are mutable and some are immutable.
The properties or contents of an instance of an immutable type may not be changed, directly. However, you can easily create another instance that is a copy of the original, with all the same property values except for any specific changes that you want to make. The newly-minted copy (with changes) must be assigned to a new, or the same, named value.
For examples of immutable types, see the five types described in the Immutable data structures section of the Library Reference. Strings are also immutable.
The simple value types (Int, Float and Boolean) are also effectively immutable. You can't change a single digit in a number in situ; you have to create a new number and assign it back to the original variable.
User-defined records are also immutable.
User-defined classes are always treated as mutable. For example, you cannot put instances of classes into a ListImmutable or DictionaryImmutable, or use them as keys in a Dictionary. This applies even if the class has no procedure members, so is effectively immutable in practice (but then you may as well use a record).
tuples cannot be directly changed once they have been made, for example you can't change just one element of a tuple. But tuples are allowed to contain mutable types, eg a List<of Int>, so they are actually treated as mutable.
The reason why mutability is of interest to computer scientists is that you can prove the correctness of programs more easily if they work with immutable data objects. It also can reduce the number of bugs in your code, and can allow more optimisations when running a program. It is widely used when writing code according to the functional programming paradigm.
Elan is a statically typed language. Every variable and parameter has a defined type.
For variables, the compiler can work out the type of the expression used to initialise the variable when it is declared. For example if you write this:
then the compiler defines the variable n to be of type
For parameters, and properties in classes and records, the programmer has to write in the type name, eg "
The return type of every function is also defined, which enables the compiler to decide the type of every expression.
To be written.
As far as we know, everything in Elan is case-sensitive. That is, no case folding is done, and the upper and lower case versions of a letter count as different characters. When you refer to a method or variable that you have defined, you must spell it exactly the same as the definition.
If case folding is desired in a running program, you can do it explicitly by calling upperCase or lowerCase.
[I wrote this before discovering the Status panel section in the IDE Guide, which covers some of the same ground]
Automatically, as you type your code, the Integrated Development Environment parses it. That is, it scans through the code trying to make sense of what it sees. Initially, the line of code is incomplete, and the Parse status in the top right hand corner shows "incomplete" on an orange background. As you type more, eventually the line will be complete enough that the parse status changes to "valid" on a green background. At this point you can type some more if you wish: for example "set n to n" is valid, but you may actually want to say "set n to n + 1".
If you type something which could never be right, however much you add, the Parse status will go to "invalid" on a red background. Any part of the line can be changed to make it right: every time you change any character, the parser starts again and re-evaluates the situation.
For example, you may be trying to write "(n + 1)*2" but initially forget about the brackets and type "n + 1", then remember the brackets and type "n + 1)". At this point it goes red, but don't be fazed: it is fine to go back to the start of the field, using the mouse or the left-arrow key, and change it to "(n + 1)", and at that point it will go green again.
Once the Parse status is valid, the compilation stage starts. The result is one of:
In either of the error states, you can click on the coloured status and it will scroll the code window to (one of) the error(s). Clicking on the erroneous line should give further information as to what the problem is. If the message is unhelpful, please let us know how it can be improved.
The unknown status usually appears if there is a name which it does not recognise. There is not much difference in practice between the "unknown" and "error" states. In neither case can the program be run.
The idea is that the "error" state can usually be corrected by editing the line with the error, but the "unknown" state may need another variable, function etc defining before it is resolved.
Elan Concepts go to the top