Starting in Qore 0.8.0, it is possible to restrict variables, class members, and function and method parameters to certain data types. This allows programmers to write safer code, as many more errors can be caught at parse time that would otherwise be caught at run time. Furthermore, providing type information to the parser allows Qore to implement performance optimizations by performing lookups and resolutions once at parse time rather than every time a variable or class member is accessed at run time.
When types are declared in a parameter list, functions and methods can be overloaded as well.
The types in the following table can be used as well as any class name or '*classname', meaning either the given class or NOTHING:
Table 2.8. Data Type Declaration Names
Name | Accepts Qore Type(s) | Returns Qore Type(s) | Description |
|---|---|---|---|
| Restricts values to Qore's Integer type. | ||
| Restricts values to Qore's Float type. | ||
| Restricts values to Qore's Boolean type. | ||
| Restricts values to Qore's String type. | ||
| Restricts values to Qore's Date type; values may be either absolute or relative date/time values. | ||
| Restricts values to Qore's Binary type. | ||
| Restricts values to Qore's Hash type. | ||
| Restricts values to Qore's List type. | ||
| Restricts values to Qore's Object type. | ||
| Restricts values to objects of the specific class given; either the class name can be given (ex: | ||
| Restricts values to Qore's NULL type; this type has few (if any) practical applications and has been included for completeness' sake. | ||
| Restricts values to Qore's NOTHING type; this type is mostly useful for declaring that a function or method returns no value. | ||
| Accepts Integer, Date and converts dates to an integer value representing milliseconds and returns the integer; incoming integers are assumed to represent milliseconds. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-integer values to an integer and returns the integer. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-float values to a float and returns the new value. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-boolean values to a boolean and returns the new value. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-string values to a string and returns the new value. | ||
| Accepts Integer, Float, Boolean, String, Date, and NULL and converts non-date values to a date and returns the new value. | ||
| all types | Accepts all types; NOTHING is returned as an empty list; a list is returned unchanged, and any other type is returned as the first element of a new list. | |
| same as received | ||
| same as received | ||
| same as received | ||
| same as received | ||
| same as received | Restricts values to Qore's Date or NOTHING type; values may be either absolute or relative date/time values. | |
| same as received | ||
| same as received | ||
| same as received | ||
| same as received | Restricts input to String, Binary, or NOTHING and returns the same type. | |
| same as received | Restricts values to closures, call references and NOTHING. | |
| Accepts Integer, Date and converts dates to an integer value representing milliseconds and returns the integer; incoming integers are assumed to represent milliseconds. If no value is passed, then NOTHING is returned. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-integer values to an integer and returns the integer. If no value is passed, then NOTHING is returned. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-float values to a float and returns the new value. If no value is passed, then NOTHING is returned. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-boolean values to a boolean and returns the new value. If no value is passed, then NOTHING is returned. | ||
| Accepts Integer, Float, Boolean, String, NULL and converts non-string values to a string and returns the new value. If no value is passed, then NOTHING is returned. | ||
| Accepts Integer, Float, Boolean, String, Date, and NULL and converts non-date values to a date and returns the new value. If no value is passed, then NOTHING is returned. | ||
| all types | Accepts all types; NOTHING and list values are returned as the same value; any other type is returned as the first element of a new list. | |
| any | same as received | Provides no restrictions on the type of value it receives and returns the same value. |
| same as received | Restricts input to String and Binary and returns the same type. | |
| same as received | Restricts values to closures and call references. | |
| reference to an lvalue | same as received | Restricts values to references to lvalues; currently only usable in function or method parameters. |
| same as received | Does not restrict value to just closures, but rather also allows call references. Synonym for | |
| same as received | Does not restrict value to just call references, but rather also allows closures. Synonym for |
Complex types (hash of lists, reference to string, etc) are currently not possible to declare.
Functions and methods can be overloaded if parameter types are declared as in the following example:
int sub example(int $i) { printf("i=%d\n", $i); return $i + 1; } string sub example(string $str) { printf("str=%s\n", $str); return $str + "foo"; }
In this case, the first version (example(int)) will be executed if called with an integer argument, and the second (example(string)) if called with a string argument.
Class methods may also be overloaded, but note that destructor(), copy(), methodGate(), memberGate(), and memberNotification() methods may not be overloaded (see Classes for more information).