[SparForte][Banner]
[Top Main Menu] Intro | Tutorials | Reference | Packages | Examples | Contributors   [Back Page]      [Next Page]  

User-defined Types

You can extend the fundamental types to create your own types.

Subtypes

The subtype statement will create a type that is compatible with the original, as if it was a renaming of the original type.

=> subtype int is integer;
=> i1 : integer := 1
=> i2 : int := 2
=> ? i1 + i2
3

In this case, "int" is equivalent to "integer" and variables of both types can be mixed freely without type casting.

Subtypes can be used where predefined types are used: you can use them to declare variables, as parameter types and as function return values.

Ada: In Ada, a subtype can be used to create a type with a smaller range of values, such as an integer with values 1 to 100. In AdaScript, the range attribute is not (yet) supported for subtypes--they are simple renamings in AdaScript.

Ada: SparForte's output subprograms (like "?", put_line, etc.) understand how to output strings and all scalar types. It work as if all necessary packages were "with"-ed and "use"-ed. In Ada, you would have to use with or instantiate I/O packages you need for each type.

Types

A regular type is logically incompatible with the type it is extend from. To make incompatible types, create a new type with the type statement.

=> type employee_number is new integer
=> type customer_number is new integer
=> en : employee_number := 1234
=> cn : customer_number := 4567
=> en := cn
en := cn;
        ^ type employee_number is not compatible with type customer_number
 

Example: Strong Typing with Logically Incompatible Types

In this case, both types are integers. However, "employee_number" variables cannot be mixed with "customer_number" variables (or other integers) without a typecast. Use new types to make sure variables that are logically different don't accidentally mix.

Types can be used where predefined types are used: you can use them to declare variables, as parameter types and as function return values.

Ada: In Ada, a type can be used to create a type with a smaller range of values, such as an integer with values 1 to 100. In AdaScript, the range attribute is not (yet) supported for types--they are simple incompatible renamings in AdaScript.

Type Casting

A type derived from the same universal type as another type can converted to the other type with a type cast. You can also use a typecast to override strong typing, provided they are the same universal type underneath. To convert the type, use the name of the type with brackets around the expression you want to convert.

=> en := employee_number( cn )

You cannot cast items derived from different universal types. For example, you cannot cast a string to an integer.

=> i : integer := integer( "this is a string" )
i : integer := integer( "this is a string" );
^ type integer (an universal_numeric) is inherently different from an universal_string

Ada:Type qualifications are not yet implemented.

[Right Submenu]

 AdaScript versus GCC

 Case Sensitivity

 Reserved Words

 Comments

 Literals

 Bourne Shell Word Expansions

 Fundamental Types

 User-defined Types

 Enumerated Types

 Arrays

 Records

 Basic Assignment

 The @ and % Operands

 Command Argument Shortcuts

 Redirection and Pipelines

 Command Line Interaction

 Built-in Shell Commands

 The Current Directory

 Database Commands

 Flow of Control

 Other Statements/ Subprograms

 External Commands

 Block Statements and Subprograms

 TCP/IP Sockets

 Numeric Formatting with Put

 Interpreter Directives

 Command Line Options

 Command Reference

 ASCII and Latin_1 Character Sets

 Common Error Messages

 Common PHP Functions and the SparForte Equivalent

[Back to Top] Back To Top [Small Forte Symbol]