Squirrel Data Types

From SigMod
Jump to navigation Jump to search
BACK
HOME
NEXT

Data stored in variables can have a number of different types which determines what type of object it is and what you can do with it. Below is the full list of data types in Squirrel, however we will only go over the primitive ones in this section. The other data types will receive their own sections later in the guide.

Data Types:

  • Null
  • Integer
  • Bool
  • Float
  • String
  • Table
  • Array
  • Function
  • Generator
  • Class
  • Instance
  • Thread
  • Userdata


To get a variable's data type as a string (see below on strings), you can use the typeof operator.

local t = typeof 5; // "integer"




Null

Has one value: null, it represents the non-existence or lack of a value.

local x; // null

local y = 5;
// ...
y = null;




Integer

Represents a 32 bit whole number, meaning it can store any value from -2,147,483,648 to 2,147,483,647. You can specify the integer base as hexadecimal by prefixing with 0x or octal with 0. You can also specify ASCII char codes between single quotes ' which will be stored as their integer value.

local x = 12345;    // Decimal
local y = 0xFFFFFF; // Hexadecimal begins with 0x
local z = 050;      // Octal begins with 0
local c = 'a';      // Char code stored as 97

These are all integer literals, which are hard coded pieces of data within a program for a specific data type. Other types have literals as well which you'll see in the examples below.




Bool

Has two values: true and false.

local is_stuck     = false;
local should_round = true;




Float

Represents a 32 bit floating point number, meaning it can store any decimal value from 1.18e-38 to 3.4e38. Due to how floating point numbers are stored in memory, they have a limited amount of accuracy for a certain number of significant digits, for a 32 bit float it's usually around 6 or 7 digits. Any additional digits for the float value may result in rounding errors and precision loss. Keep this in mind when you're working with floats.

local float1 = 1.23456;  // 1.23456 (Regular float literal)
local float2 = 1.2e34;   // 1.2e+34  (Scientific notation float literal)

// Precision loss
local float3 = 1.234567; // 1.23457
local float4 = 1234.567; // 1234.57
local float5 = 123456.7; // 123457
local float6 = 12345678999.0; // 1.23457e+10

You cannot have a leading floating point for float literals, they must start with at least one digit.

// Invalid
local float1 = .123;
local float2 = .12E5;

// Valid
local float3 = 0.123;
local float4 = 0.12E5;




String

Strings are a sequence of characters with any length between quotation marks ". Strings are immutable, meaning they cannot be modified and you must create a new string if you need to change the old one. Regular strings must begin and end on the same line, they cannot contain any new line characters (\n).

// Strings ...
local str1 = "The quick brown fox jumps over the lazy dog."; // String
local str2 = "123"; // String with number characters, not an integer
local str3 = "a";   // String
local str4 = "";    // Empty string

// ... compared to char codes
local int1 = 'a';   // NOT a string
local int2 = '';    // Compile error
local int3 = ''';   // Compile error
local int4 = '\'';  // 39, Valid char code for ' (explained below)

// Need to make a new string if we want to modify
local name = "bar";
name = "barber";


Strings (and char literals) may contain escape sequences, which are special characters that denote that something special should happen. Even though they have multiple characters in their definition, they count as one character when actually in use. Here are the ones you're most likely to use:

  • \t - Display a tab
  • \n - Move to a new line
  • \\ - Insert \ char
  • \" - Insert " char
  • \' - Insert ' char
local text  = "Hello there! Please enter your class:\t";    // Tab
local text2 = "What do you mean you don't \"have one\"?\n"; // " char and new line


There may come a time when you need a string that does not interpret escape sequences or that can span multiple lines, which is where verbatim strings come in handy. Verbatim strings begin with @" and end with a regular quote ".

local mystring = @"eenie
meenie
minie
moe";

You may also need to add quote characters within the verbatim string, to do so you double up each quotation "". The double quote will be replaced with a single quote by the compiler.

local mystring = @"eenie
""meenie""
minie
moe";