The Basics
Constants and Variables
Constants and variables associate a name (such as maximumNumberOfLoginAttempts
or welcomeMessage
) with a value of a particular type (such as the number 10
or the string "Hello"
). The value of a constant can’t be changed once it’s set, whereas a variable can be set to a different value in the future.
Declaring Constants and Variables
Constants and variables must be declared before they’re used. You declare constants with the Const
keyword and variables with the Var
keyword. Here’s an example of how constants and variables can be used to track the number of login attempts a user has made:
This code can be read as:
"Declare a new constant called maximumNumberOfLoginAttempts
, and give it a value of 10
. Then, declare a new variable called currentLoginAttempt
, and give it an initial value of 0
."
In this example, the maximum number of allowed login attempts is declared as a constant, because the maximum value never changes. The current login attempt counter is declared as a variable, because this value must be incremented after each failed login attempt.
You can declare multiple variables on a single line, separated by commas:
or shorter:
If a stored value in your code won’t change, always declare it as a constant with the Const
keyword. Use variables only for storing values that need to be able to change.
Type Annotations
You can provide a type annotation when you declare a constant, to be clear about the kind of values the constant can store. Write a type annotation by placing a colon after the constant name, followed by a space, followed by the name of the type to use.
This example provides a type annotation for a variable called welcomeMessage
, to indicate that the variable can store String
values:
The As
in the declaration means "…of type…," so the code above can be read as:
"Declare a constant called welcomeMessage
that’s of type String
."
The phrase "of type String
" means "can store any String
value." Think of it as meaning "the type of thing" (or "the kind of thing") that can be stored.
The welcomeMessage
constant can now be set to any string value without error:
You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:
Naming Constants and Variables
Constant and variable names can contain almost any character, including Unicode characters:
Constant and variable names can’t contain whitespace characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.
Once you’ve declared a constant or variable of a certain type, you can’t declare it again with the same name, or change it to store values of a different type. Nor can you change a constant into a variable or a variable into a constant.
You can change the value of an existing variable to another value of a compatible type. In this example, the value of friendlyWelcome
is changed from "Hello!"
to "Bonjour!"
:
Unlike a variable, the value of a constant can’t be changed after it’s set. Attempting to do so is reported as an error when your code is compiled:
Comments
Use comments to include nonexecutable text in your code, as a note or reminder to yourself. Comments are ignored by the Xojo compiler when your code is compiled.
Comments in Xojo are very similar to comments in C. Single-line comments begin with two forward-slashes (//
), '
or REM
:
Integers
Integers are whole numbers with no fractional component, such as 42
and -23
. Integers are either signed (positive, zero, or negative) or unsigned (positive or zero).
Xojo provides signed and unsigned integers in 8, 16, 32, and 64 bit forms. These integers follow a naming convention similar to C, in that an 8-bit unsigned integer is of type UInt8
, and a 32-bit signed integer is of type Int32
.
Integer Bounds
Integer
In most cases, you don’t need to pick a specific size of integer to use in your code. Xojo provides an additional integer type, Integer
, which has the same size as the current platform’s native word size:
On a 32-bit platform,
Integer
is the same size asInt32
.On a 64-bit platform,
Integer
is the same size asInt64
.
Unless you need to work with a specific size of integer, always use Integer
for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Integer
can store any value between -2,147,483,648
and 2,147,483,647
, and is large enough for many integer ranges.
UInteger
Xojo also provides an unsigned integer type, UInteger
, which has the same size as the current platform’s native word size:
On a 32-bit platform,
UInteger
is the same size asUInt32
.On a 64-bit platform,
UInteger
is the same size asUInt64
.
Use UInteger
only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this isn’t the case, Integer
is preferred, even when the values to be stored are known to be nonnegative. A consistent use of Integer
for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described in Type Safety and Type Inference.
Floating-Point Numbers
Floating-point numbers are numbers with a fractional component, such as 3.14159
, 0.1
, and -273.15
.
Floating-point types can represent a much wider range of values than integer types, and can store numbers that are much larger or smaller than can be stored in an Integer
. Xojo provides two signed floating-point number types:
Double
Single
The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, Double
is preferred.
Type Safety and Type Inference
Xojo is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code requires a String
, you can’t pass it an Integer
by mistake.
Because Xojo is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.
Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every constant that you declare. If you don’t specify the type of value you need, Xojo uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.
Type inference is particularly useful when you declare a constant with an initial value. This is often done by assigning a literal value (or literal) to the constant at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42
and 3.14159
in the examples below.)
For example, if you assign a literal value of 42
to a new constant without saying what type it is, Xojo infers that you want the constant to be an Integer
, because you have initialized it with a number that looks like an integer:
Likewise, if you don’t specify a type for a floating-point literal, Xojo infers that you want to create a Double
:
Xojo always chooses Double
(rather than Single
) when inferring the type of floating-point numbers.
If you combine integer and floating-point literals in an expression, a type of Double
will be inferred from the context:
The literal value of 3
has no explicit type in and of itself, and so an appropriate output type of Double
is inferred from the presence of a floating-point literal as part of the addition.
Numeric Literals
Integer literals can be written as:
A decimal number, with no prefix
A binary number, with a
&b
prefixAn octal number, with a
&o
prefixA hexadecimal number, with a
&h
prefix
All of these integer literals have a decimal value of 17
:
Numeric Type Conversion
Use the Integer
type for all general-purpose integer constants and variables in your code, even if they’re known to be nonnegative. Using the default integer type in everyday situations means that integer constants and variables are immediately interoperable in your code and will match the inferred type for integer literal values.
Use other integer types only when they’re specifically needed for the task at hand, because of explicitly sized data from an external source, or for performance, memory usage, or other necessary optimization. Using explicitly sized types in these situations helps to catch any accidental value overflows and implicitly documents the nature of the data being used.
Integer Conversion
The range of numbers that can be stored in an integer constant or variable is different for each numeric type. An Int8
constant or variable can store numbers between -128
and 127
, whereas a UInt8
constant or variable can store numbers between 0
and 255
. A number that won’t fit into a constant or variable of a sized integer type is reported as an error when your code is compiled:
Booleans
Xojo has a basic Boolean type, called Boolean
. Boolean values are referred to as logical, because they can only ever be true or false. Xojo provides two Boolean constant values, True
and False
:
The types of orangesAreOrange
and turnipsAreDelicious
have been inferred as Boolean
from the fact that they were initialized with Boolean literal values. As with Integer
and Double
above, you don’t need to declare constants or variables as Boolean
if you set them to True
or False
as soon as you create them. Type inference helps make Xojo code more concise and readable when it initializes constants or variables with other values whose type is already known.
Boolean values are particularly useful when you work with conditional statements such as the If
statement:
Conditional statements such as the if
statement are covered in more detail in Control Flow.
Xojo’s type safety prevents non-Boolean values from being substituted for Boolean
. The following example reports a compile-time error:
However, the alternative example below is valid:
The result of the i = 1
comparison is of type Boolean
, and so this second example passes the type-check. Comparisons like i = 1
are discussed in Basic Operators.
As with other examples of type safety in Xojo, this approach avoids accidental errors and ensures that the intention of a particular section of code is always clear.
Last updated