Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to create a user-defined data type in Euphoria



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 type
         return y >= 18 and y <= 50 --setting the legal values
     end type
age h1, h2 --this is how you can declare variables of type age 
h1 = 40
--h2 = 100
print(1,  h1)
puts(1, '\n')   
--using these user defined types in functions and others
procedure what_age(age your_age)
  printf(1,"You are %d years young",your_age)
end procedure


In the code above:

  • Line 1 We declare a type: age with the base type of int.
  • Line 2 We return the allowable value for the type created.
  • Line 4: We declare two variables h1 and h2 as age type.
  • Lines 5 and 6: We assign values to the variables. Although line 6 is commented out because it causes a type check error to be thrown because the value is not allowed.
  • Line 10: We create a procedure what_ageand pass your_age as a parameter, which is of type age.
  • Line 12: We end the procedure with end procedure.
  • Line 13: We make a call to the procedure.



View all Courses

Keep Exploring