Skip to main content

Post 3: Constants, Variables, and Arithmetic Operations

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:
  • Addition, +
  • Subtraction, -
  • Multiplication, *
  • Division, /
  • Modulo, %

Iterating Integers

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.


Comments

  1. 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?

    ReplyDelete
    Replies
    1. Since 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

Post a Comment

Popular posts from this blog

Post 5: Repeated Action Syntax

Loops While Loops While loops execute while the given conditional remains true. While loops use the same syntax as while loops in C# and Java.  The code will cease to execute once the given conditional is no longer true. For Loops For loops execute for a set, given length. A for loop will iterate through a range of given integers until the end of that range is reached. For loops are written the same was as in Java and C#.  For loops have the benefit of being able to keep track of how far something has looped through without the need for a separate variable by using the 'i', or however it may be named. For loops do not need to strictly follow hard coded numbers and could loop through the length of an array or size of a list or to any integer value. Iteration Iteration is a key function of lists in Fantom. By using the 'listName.each' method, you are able to iterate through the length of the list and execute code for each item in that list. This c...

Post 4: Conditional Statements

Conditionals Conditionals in Fantom are comprised of if-statements, if-else statements, and switch statements. If-statements If-statements in Fantom remain virtually the same to if-statements in it's parent languages, Java and C#. The if-statement takes some form of conditional and will only perform the block of code inside of it IF the condition stated is true. Every if-statement will be checked if performed in a sequence, however if-else statements will stop once the first true condition is met and perform the nested code. Else will only be performed if none of the previous conditionals have been satisfied. Switch Statements Switch statements remain very similar to Java and C# as well, however with some syntax differences. A switch condition is declared at the start and is compared to the list of case labels below it, executing code when a condition is met. If none of the conditionals are met, there is a default condition that works like the else of and if-statement....

Post 1: Background Information and Resources

Language Background Fantom is an object-oriented programming language designed to work in both the Java Runtime Environment and in the .NET Common Language Runtime while providing a more refined API that maintains much of the same functionality.  Fantom's syntax is relatively similar to that of Java and C# while refining certain aspects of the syntax for functionality and simplicity.  System.out.println("Hello world!"); becomes  echo("Hello world!") Fantom is designed to be portable between the Java and .NET VMs while also holding support for being compiled to JavaScript for browser use, making Fantom versatile.  Creators Fantom was created in 2005 by Brian Frank and Andy Frank and was originally named Fan. When the language began to gain some popularity, the name was changed to Fantom in order to address concerns about the ability to search for resources on a language named "Fan". The creators say that Fantom was created in orde...