std: Basic services
This module defines several basic types, constants and utility functions. The types include the primitive types, basic collection types and several exception types. The std module is the only module that is always imported without an explicit import definition, and its services are used by practically every Alore program.
Object type
- Object
- The common supertype of every other class
Primitive types
- About primitive types
- The shared properties of primitive types
- Int
- The integer type
- Str
- The string type
- Float
- The floating-point number type
- Boolean
- The Boolean type
- Type
- The type of types
- Function
- The type of functions and bound methods
- Constant
- The symbolic constant type
- Pair
- A pair of values
- Range
- A range of integer values
Collection types
- Array
- A resizable array of values
- Tuple
- A fixed-length, immutable sequence of values
- Map
- A map of key-value pairs implemented as a hash table
Functions
- Write(...)
- Write the arguments to the StdOut stream. This is a shorthand for io::StdOut.write.
- WriteLn(...)
- Write the arguments to the StdOut stream, followed by a line break. This is a shorthand for io::StdOut.writeLn.
- ReadLn()
- Read a line from StdIn, and return the line without the line ending. This is a shorthand for io::StdIn.readLn.
- Repr(object)
- Return a representation of the argument as a string. This is similar to the Str constructor, but this function may return more detailed information. Repr works with objects of all types. First try to return the result of calling the _repr method without arguments. If that fails, return the result of the _str method. If even that fails, return a generic representation of the argument that indicates the type of the object.
- Chr(n)
- Return a single-character string with character code n. The valid range of values for n is from 0 to 65535, inclusive.
- Ord(ch)
- Return the character code of a single-character string. The return value is an integer in the range from 0 to 65535, inclusive.
- Abs(x)
- Return the absolute value of an integer or a float.
- Min(x, y)
- Return the minimum of two objects.
- Sort(sequence[, func])
- Return an array that has the items of a sequence sorted in ascending order (by default). The optional argument specifies a function that can be used for evaluating the order of two items. The function must accept 2 arguments and return a boolean indicating whether the first argument should appear earlier than the second argument in the sorted sequence.
- Hash(object)
- Return the hash value of an object as an integer.
If the object has the _hash() method, return the result of
calling that method with no arguments. This method should return an
integer, and all instances that compare equal should receive the same hash
value. If the object does not have this method, calculate the hash value
based on the identity of the object.
The main purpose of the hash value is to spread different objects in different slots of a hash table most of the time, and to collect equal objects in the same slots. Therefore, even though technically all objects of a certain type could have the same hash value, this would result in a dramatic slowdown of certain operations. Due to efficiency reasons, the optimal maximum magnitude of hash values is typically about 2**29.
- Exit([value])
- Exit the program. The optional argument specifies an integer return value to the operating system. The function is implemented by raising an ExitException. It can be caught using a try statement to prevent the program from exiting.
Constants
This module also defines boolean constants:
A few string constants are provided as well:
- Tab
- The tab character (code 9).
- Newline
- The platform specific line break string. On Unix-like operating systems it is LF and on Windows CR + LF.
- LF
- The line feed character (code 10).
- CR
- The carriage return character (code 13).
Exceptions
The exception classes in the std module form this inheritance hierarchy:
- Exception
- ValueError
- TypeError
- MemberError
- ArithmeticError
- IndexError
- CastError
- ResourceError
- MemoryError
- RuntimeError
- IoError
- InterruptException
- ExitException
- ValueError
Descriptions of the exception classes:
- class Exception([message])
- The base class of all exceptions. Exception has a single member, message, that contains the error message argument given to the constructor, or nil if the message argument was omitted.
- class ValueError([message])
- Raised when the input values of an operation or a function have invalid types or values. Typically, ValueError signifies a programming error, but it can be caught to recover from these situations. The several subclasses of ValueError indicate more specific conditions.
- class TypeError([message])
- Raised when the inputs of an operation or a function have unsupported types. Note that MemberError will be often raised in similar situations.
- class MemberError([message])
- Raised when an object does not have a requested member or the member being assigned to cannot be modified.
- class ArithmeticError([message])
- Raised when an arithmetic operation or function receives invalid inputs. A typical example is division by zero.
- class IndexError([message])
- Raised when the index of a sequence object such as an array is out of bounds or otherwise invalid.
- class CastError([message])
- Raised when a cast operation fails.
- class ResourceError([message])
- Raised when a resource is exhausted. This exception can be triggered by lack of disk space or by too many open files, for example.
- class MemoryError([message])
- Raised when a memory allocation request cannot be fulfilled. This exception may also be raised if only a specific kind of memory pool is exhausted, even though there might be memory available for other purposes.
- class RuntimeError([message])
- Raised when there is a run-time error during execution that does not have a more specific exception class.
- class IoError([message])
- Raised as a response to an operating system input/output error. This exception is used by several modules such as io and os. Typical reasons for raising an IoError include a missing file or directory, insufficient access rights to a resource, disk failure and insufficient free space on a device.
- class InterruptException([message])
- Raised when the user interrupts the program by pressing Ctrl+C.
- class ExitException(code)
- Raised by the std::Exit function. The argument specifies the integer return value reported to the operating system.