Constants, Variables, and Arithmetic Operations
Variables Types
Variables that would be considered primitives in Java (int, bool, float, etc.) are sub-classes of the Object class in Fantom. This allows them to contain various useful methods and allows for Fantom to remain fully object-oriented without sacrificing any performance gains from primitives. This follows the .NET standard of handling primitives.
The Following are all of the Variable Types I will cover in this blog post:
- Bool
- Int
- Float
- Decimal
- Str
- Duration
- Uri
- Range
- List
When declaring variables in Fantom, ':=' is used instead of '='. However, when updating a variables value, '=' should be used instead of ':='.
Variables in Fantom don't need to be assigned a variable type during declaration. Both of these will compile fine:
Integers
The Integer variable type can hold any positive or negative whole number ranging from 9,223,372,036,854,775,807 to -9,223,372,036,854,775,808. The Integer class in Fantom is 64 bit and combines the int and long primitives of Java into one class. Integers will be initialized as '0' by default. Meaning 'Int num;' will be equal to '0'. However, if a null initialization is desired, this can be obtained by using a '?' after Int. Therefor, 'Int? num' will be null.
Floats and Decimals
The Float variable type is a 64 bit representation of a floating point number. Decimals are also 64 representations of floating point numbers, however Decimal math is more precise than Float math within Fantom and is preferred for that reason. When declaring a Float or Decimal variable, the value you are setting the variable to must have a 'f' or 'd' immediately following it respectively. This is not case sensitive.
Durations
The duration variable type in Fantom is a Long number used to represent a time duration in nanoseconds. The function Duration.make() can be used to create a duration, or Duration() can be used as shorthand.
Uris
A Uri, or Uniform Reference Identifier, is a variable that stores some URL using it's own syntax different than that of a String. The Uri uses back ticks (`) instead of single quotes ('). These can be used to store the location of a file or a web URL. These can be easily converted to strings by using the uri.toString() method.
Ranges
The Range variable type is used to store, well, ranges. Ranges are declared using a '..' to represent the difference. The end number is inclusive is just '..' is used, however '..<' can be used as well to make the end number exclusive instead.
Lists
Lists allow for a collection of one type of variable to be kept in one place. There is no fixed size to a list and you are able to index, add, remove, insert, or do any number of functions to a list. A list object can be declared as such or can be declared as a specific type of variable. Similarly to ArrayLists and Arrays in Java.
More Resources
A complete list of functions contained within each variable class can be found here on the official documentation.
Constants
Any variable type can become a constant variable. These cannot be done in one line like they can in Java. The object must use the object function '.toImmutable' in order to become a constant 'final' variable. These cannot be changed later in the program.
Arithmetic Operations
Fantom is able to calculate:
There are multiple different methods for iterating integers, including shorthand methods. All of the following change the integer by 1 in the positive and negative directions accordingly.










Is it possible for Fantom lists to hold more than one type of variable in a list, or is each list restricted to holding one type of variable? How would a list of multiple types of variables be declared if this is possible?
ReplyDeleteSince all of the variables in Fantom are part of the Object class, it is possible to create a list of Objects that can store any type of variable in it. It could be declared as "List objectListName := ["Hello world", 7, 1.8F]" or simply "List objectListName := Obj[,]" for an empty object list.
Delete