A data type is used in Euphoria to specify the allowable type of data for a particular variable. Euphoria Language is a static typed language. This means that every variable must be declared, stating the data type first before assigning any value and using such a variable.
Users can be more specific about the data type in their programs by defining their own data types on-top of the existing Euphoria primitive types.
This is achieved using the type()
function following the syntax that is explained below.
The type()
function is utilized to create a custom user type. This function takes the identifier for the variable to be typed, with the function body containing any rule for the data type variables as arguments.
type type_identifier(declare_base_type)
--optional range of legal values
end type
type
: Keyword used to call the type function.type_identifier
: The name we wish to give the new type to be created optional range or legal values
: The rule that governs the legal value if specified.declare_base_type
: Any of the basic types available in Euphoria. Every custom type usually has base types on top of which they are created.end type
: The statement that comes to the end of the type creation.In the demonstration below, there is a sample creation of a custom type age
. And how it is used in a procedure, which is also possible in a function and other expressions:
type age(integer y) --declaring a custom data typereturn y >= 18 and y <= 50 --setting the legal valuesend typeage h1, h2 --this is how you can declare variables of type ageh1 = 40--h2 = 100print(1, h1)puts(1, '\n')--using these user defined types in functions and othersprocedure what_age(age your_age)printf(1,"You are %d years young",your_age)end procedurewhat_age(50)
In the code above:
type: age
with the base type of int
.h1
and h2
as age
type.what_age
and pass your_age
as a parameter, which is of type age
.end procedure
.