2.11. Data Type Declarations

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

int

Integer

Integer

Restricts values to Qore's Integer type.

float

Float

Float

Restricts values to Qore's Float type.

bool

Boolean

Boolean

Restricts values to Qore's Boolean type.

string

String

String

Restricts values to Qore's String type.

date

Date

Date

Restricts values to Qore's Date type; values may be either absolute or relative date/time values.

binary

Binary

Binary

Restricts values to Qore's Binary type.

hash

Hash

Hash

Restricts values to Qore's Hash type.

list

List

List

Restricts values to Qore's List type.

object

Object

Object

Restricts values to Qore's Object type.

<classname>

Object

Object

Restricts values to objects of the specific class given; either the class name can be given (ex: Mutex or a qualified path to the class: Qore::Mutex).

null

NULL

NULL

Restricts values to Qore's NULL type; this type has few (if any) practical applications and has been included for completeness' sake.

nothing

NOTHING

NOTHING

Restricts values to Qore's NOTHING type; this type is mostly useful for declaring that a function or method returns no value.

timeout

Integer, Date

Integer

Accepts Integer, Date and converts dates to an integer value representing milliseconds and returns the integer; incoming integers are assumed to represent milliseconds.

softint

Integer, Float, Boolean, String, NULL

Integer

Accepts Integer, Float, Boolean, String, NULL and converts non-integer values to an integer and returns the integer.

softfloat

Integer, Float, Boolean, String, NULL

Float

Accepts Integer, Float, Boolean, String, NULL and converts non-float values to a float and returns the new value.

softbool

Integer, Float, Boolean, String, NULL

Boolean

Accepts Integer, Float, Boolean, String, NULL and converts non-boolean values to a boolean and returns the new value.

softstring

Integer, Float, Boolean, String, NULL

String

Accepts Integer, Float, Boolean, String, NULL and converts non-string values to a string and returns the new value.

softdate

Integer, Float, Boolean, String, Date, NULL

Date

Accepts Integer, Float, Boolean, String, Date, and NULL and converts non-date values to a date and returns the new value.

softlist

all types

List

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.

*int

Integer or NOTHING

same as received

Restricts values to Qore's Integer or NOTHING types.

*float

Float or NOTHING

same as received

Restricts values to Qore's Float or NOTHING types.

*bool

Boolean or NOTHING

same as received

Restricts values to Qore's Boolean or NOTHING types.

*string

String or NOTHING

same as received

Restricts values to Qore's String or NOTHING types.

*date

Date or NOTHING

same as received

Restricts values to Qore's Date or NOTHING type; values may be either absolute or relative date/time values.

*binary

Binary or NOTHING

same as received

Restricts values to Qore's Binary or NOTHING types.

*hash

Hash or NOTHING

same as received

Restricts values to Qore's Hash or NOTHING types.

*list

List or NOTHING

same as received

Accepts either a List or NOTHING.

*data

String, Binary, or NOTHING

same as received

Restricts input to String, Binary, or NOTHING and returns the same type.

*code

Closure, Call Reference or NOTHING

same as received

Restricts values to closures, call references and NOTHING.

*timeout

Integer, Date or NOTHING

Integer or 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.

*softint

Integer, Float, Boolean, String, NULL or NOTHING

Integer or NOTHING

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.

*softfloat

Integer, Float, Boolean, String, NULL or NOTHING

Float or NOTHING

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.

*softbool

Integer, Float, Boolean, String, NULL or NOTHING

Boolean or NOTHING

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.

*softstring

Integer, Float, Boolean, String, NULL or NOTHING

String or NOTHING

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.

*softdate

Integer, Float, Boolean, String, Date, NULL or NOTHING

Date or NOTHING

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.

*softlist

all types

List or NOTHING

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

any

same as received

Provides no restrictions on the type of value it receives and returns the same value.

data

String or Binary

same as received

Restricts input to String and Binary and returns the same type.

code

Closure, Call Reference

same as received

Restricts values to closures and call references.

reference

reference to an lvalue

same as received

Restricts values to references to lvalues; currently only usable in function or method parameters.

closure

Closure, Call Reference

same as received

Does not restrict value to just closures, but rather also allows call references. Synonym for code.

callref

Closure, Call Reference

same as received

Does not restrict value to just call references, but rather also allows closures. Synonym for code.


Complex types (hash of lists, reference to string, etc) are currently not possible to declare.

2.11.1. Overloading

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).