Values and Types
Gravity is a dynamically typed language so variables do not have a type, although they refer to a value that does have a type. In Gravity everything is an object (with methods you can call and instance variables you can use). Basic intrinsic types are of class Object, Int, Float, String, Bool, Null, Class, Function, Fiber, Instance, List, Map and Range.
Object
Object is the root class of every object inside Gravity. Through the Object class, objects inherit a basic interface to the runtime system and the ability to behave as Gravity objects.
Int
An Int represents a 64 bits (can optionally be compiled as 32bit) signed number:
var a = 123; // decimal
var b = 0xFF; // hexadecimal
var c = 0O7777; // octal
var d = 0B0101; // binary
Float
A float represents a 32 bits (or better) floating point number:
var a = 3.1415; // float
var b = 1.25e2; // scientific notation
String
Strings are an immutable sequence of characters. String literals can be surrounded in double or single quotes.
var a = "Hello World"; // double quotes
var b = 'Hello World'; // single quotes
// Strings have also a length property
var n = b.length; // n is now 11
Bool
The bool data type can have only two values, they are the literals true and false. A Bool value expresses the validity of a condition (tells whether the condition is true or false).
var a = true;
var b = false;
Null
It indicates the absence of a value. If you call a method that doesn’t return anything and get its returned value, you get null back. The null data type is also used to initialize uninitialized variables with a default value.
class Newton {
var mass = 10;
var acceleration;
func force() {
return mass * acceleration;
}
}
func f2() {
var sir = Newton();
// acceleration instance variable has no default value
// so it is automatically set to null
return sir.force();
}
func f1() {
var a;
// a is uninitialized so it has a default null value
return a;
}
Range
A range is an object that represents a consecutive range of numbers. Syntax for this type has been directly inspired by Swift.
// a represents a range with values 1,2,3
var a = 1...3;
// b represents a range with values 1,2
var b = 1..<3;
// Ranges have also a conveniente count property
var n1 = a.count; // n1 is now 3
var n2 = b.count; // n2 is now 2