Basic Types
Numbers
In GreyCat we have int and float to represent numbers, they are both stored as 64bits values under-the-hood.
fn main() {
var i = 42; // an integer number known as 'int' in GreyCat (64-bits)
var pi = 3.14; // a floating-point number known as 'float' in GreyCat (64-bits)
var c = 3f; // a floating-point number, with explicit 'f' suffix
var x = float::max; // get the highest floating point
var y = 1e7 // scientific notation
var z = 1_000_000 // underscore for more readable numbers
}
| Name | Symbol | Min | Max | Precision |
|---|---|---|---|---|
| Integer (64 bits) | int | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 1 |
| Float (64 bits) | float | -1.797E308 | 1.797E308 | 5.4E-079 |
When using equality check (==) with floats, we use Strict floating point comparison, keep this in mind when handling float point numbers, if needed use Assert::equalsd that takes an epsilon for less stricy comparison.
Booleans
Booleans are specified with bool.
var isTrue = true;
var isFalse = false;
You also have your classical logical operators on booleans.
&&OR||AND!NOT
println(isTrue && isFalse); // false
println(isTrue || isFalse); // true
println(!isFalse); // true
Characters
The char type is defined with singlequote ’
var myChar = 'a';
String
The String type is not considered a primitive type in GreyCat, you will remark that all primitives start with a lowercase where as objects start with an uppercase.
fn main() {
var name = "Luxembourg";
var sentence = "My city is named: ${name}";
println(sentence); // My city is named: Luxembourg
}
String templates
For convenience, GreyCat natively offers a template mechanism ${ } , any GreyCat expression can be interpreted in this template.
fn main() {
var name = "Luxembourg";
var sentence = "${name} has ${name.size()} characters count in it";
println("${sentence}"); // Luxembourg has 10 characters count in it
}
String Dictionary
All string literals in GreyCat (who do not use the template syntax, meaning no ${}) are put into a dictionary and mapped to an int internally when compiled.
Every time you access that string it will not allocate additional space, and every equality checks become more performant.
var val = "My String" // This is an optimized string literal
var val2 = "My Second String ${val}" // This is not
Geographic data
GreyCat stores values of type geo in an efficient way: 64-bits for both latitude and longitude.
fn main() {
var position = geo { 49.6116, 6.1319 };
println(position);
println(position.lat());
println(position.lng());
}
We also offer some aditonal types to handle shapes made of geo points:
GeoCircleGeoBoxGeoPoly
All three types offer a contains(point:geo):bool function which expect a geoas input and return a bool to indicate if the given point is contained within the area defined by the shape.
GeoCirlcleis defined by a center geoand a floatradius (degrees).
GeoBoxis defined by a south-west and north-east corner geo.
GeoPoly is defined by an array of geo points that outline the polygon.
Both GeoPolyand GeoCircle offer function to retrieve the south-west (sw():geo) and north-east (ne():geo) point of the bounding box containing the shape.