Primitive data types
Spice supports eight different primitive data types out of the box: double
, int
, short
, long
, byte
, char
, string
and bool
. In addition, there is a builtin type-inferred type, called dyn
. Let us take a look at each one individually!
The double
data type¶
Doubles are signed double-precision 64-bit IEEE 754 floating point numbers.
Variables¶
In Spice, variables of type double
can be defined like this:
Literals¶
Double literals require a decimal separator, may have a preceding sign and the scientific notation is supported.
They must match the following regular expression:
Text Only | |
---|---|
Examples:
The int
data type¶
Integers are whole numbers of 32-bit. Signed integers have a range from a min of -2,147,483,648 to a max of 2,147,483,647. Unsigned integers have a range from a min of 0 to a max of 4,294,967,295.
Variables¶
In Spice, variables of type int
can be defined like this:
Spice | |
---|---|
Literals¶
Integer literals may have a preceding sign. The literal can be noted in decimal, binary, hexadecimal or octal system, each with its respective prefix and number/letter range. For unsigned int literals, the suffix u
can be appended. Scientific notation is currently not supported.
They must match the following regular expression:
Text Only | |
---|---|
Examples:
The short
data type¶
Shorts are whole numbers of 16-bit. Signed shorts have a range from a min of -32,768 to a max of 32,767. Unsigned shorts have a range from a min of 0 to a max of 65535.
Variables¶
In Spice, variables of type short
can be defined like this:
Spice | |
---|---|
Literals¶
Short literals may have a preceding sign. The literal can be noted in decimal, binary, hexadecimal or octal system, each with its respective prefix and number/letter range. For unsigned short literals, the suffix u
can be appended. The literal must end with the suffix s
. Scientific notation is currently not supported.
They must match the following regular expression:
Text Only | |
---|---|
Examples:
The long
data type¶
Longs are whole numbers of 64-bit. Signed long have a range from a min of -9,223,372,036,854,775,808 to a max of 9,223,372,036,854,775,807. Unsigned integers have a range from a min of 0 to a max of 18,446,744,073,709,551,615.
Variables¶
In Spice, variables of type long
can be defined like this:
Spice | |
---|---|
Literals¶
Long literals may have a preceding sign. The literal can be noted in decimal, binary, hexadecimal or octal system, each with its respective prefix and number/letter range. For unsigned long literals, the suffix u
can be appended. The literal must end with the suffix l
. Scientific notation is currently not supported.
They must match the following regular expression:
Text Only | |
---|---|
Examples:
The byte
data type¶
Bytes are unsigned whole numbers of 8-bit, which have a range from a min of 0 to a max of 255.
Variables¶
In Spice, variables of type byte
can be defined like this:
Literals¶
There are no byte literals in Spice. Use type casting to cast an int, short or long literal to a byte.
Spice | |
---|---|
The char
data type¶
Chars are unsigned whole numbers of 8-bit, which have a range from a min of 0 to a max of 255. The value of a char represents the UTF-8 sign of the 8-bit integer value.
Variables¶
In Spice, variables of type char
can be defined like this:
Literals¶
Char literals are noted either as a single character or a single escape sequence, enclosed in single quotes.
They must match the following regular expression:
Text Only | |
---|---|
Examples:
The string
data type¶
Strings are immutable sequences of 8-bit integers (chars) and contain text-like information. The length of a string is unlimited.
Note
This type of string is immutable. So if you want to mutate your string at runtime, you can use the builtin String data type.
Tip
Use the string
primitive type over the String
builtin type as much as possible, due to its advantages in runtime performance. The usage of the String
builtin type is only recommended, when you need to modify the value of the string at runtime.
Variables¶
In Spice, variables of type string
can be defined like this:
Literals¶
String literals are noted as a sequence of character and/or escape sequences, enclosed in double quotes.
They must match the following regular expression:
Text Only | |
---|---|
Examples:
The bool
data type¶
Booleans are 1-bit integers and can be assigned with the exactly two values: true
or false
.
Additional information
Many language components like if statements, for loops, while loops, etc. use the bool
data type as evaluation unit for conditions. You can find more information about that in the respective sections.
Variables¶
In Spice, variables of type bool
can be defined like this:
Literals¶
Bool literals are either the keyword true
or false
.
The dyn
data type¶
The dyn
data type (dyn
stands for dynamic) is a more unconventional data type. The concrete type of a dyn
variable or field gets inferred at compile time so that the language stays type-safe. This also means, that once you assign a value to a dyn
variable, the type is pinned and can't be inferred anymore. Assigning a value of another type to the same variable will result in a compile error.
Usage of the dyn data type
The dyn data type can not be used everywhere. Function arguments can only be declared as dyn
, when they have a default value attached to them, from which the type can be inferred. For more information about functions, please visit the respective documentation section.
Variables¶
Dyn variables can be defined like this: