A type object defines which data entities are accepted as filler for an instance variable or arguments to a method. Type objects are part of the definition of both methods and variables.
Types are normally specified using a name. The type<-convert
method translates type-names into (reusable) type
objects. See type<-convert
for the type syntax.
If a type is used to specify the arguments to a method, it can also
specify the name of the argument. See type<-argument_name.
Argument names are used for documentation purposes as well as to allow
for the specification of arguments by name rather than by position. See
class := for details.
Bugs:
Currently, the semantics of a type is determined by the type<->kind
attribute and no new kinds may be introduced.
Type objects have no protection against changes to them, but they may not be changed, except as part of their definition process.
<-kind:
alias. type<-context
contains the real type specification.<-translate_function
and type<-validate_function.
The contents of this field is determined by these functions, and
therefore by the type<->kind
attribute.
<-convert.<-kind
attribute determines the -validate_function and -translate_function
pointers and thus interpretation of the context attribute. Its values
are (between brackets the the syntax construct; see also type<-convert).
Objects (instances of classes):
<-convert
method of the class.
<-convert.
Special types:
|int’(i.e.
no function
objects). Translate: Evaluates functions.
Numeric values:
\n, \t, \r, \f, \b
with their usual meaning
^<char> for control characters
\C-<char> for control characters
tuple(low, high).
Validate: Checks range (including low and high). Translate: Translates
to int and validates range,
tuple(low, high).
Validate: Checks range (including low and high). Translate: Translates
to real and validates range.
Events:
char or event-name (see @event_tree).
Set types:
<-member.
Because type<-context
does not allow for a function, functions should be passed embedded in a quote_function
object. The function
|<type name 2>|...]
Validate: Just validates the super-types. Translate: According to the
super-types.
Type-name alias and argument names:
<-context.
Translate: as type<-context.
Internal use:
->validate
and
type<-translate
after its own validation or translation fails.
-kind
@see type<-translate-kindvarargs type
by a method. This implies any number of datums of this type is allowed.
<-lookup).
After creation, the type is object->protect’ed
and a mapping from the type type<-fullname
to the type object is
added to the hash_table
object @types.
The arguments are:
<-fullname
of the new type. This name will be added to the type name-table and must
be unique.
<-kind
Determines the validate and translate behaviour (the interpretation of
the context).
<-context
The context value. See type->kind
for details
<-supers
Alternative types of datums accepted
Types are normally created by converting names (see type<-convert)
or
type<-copy
to create synonym names for a complex type.
<-validate_function.
When this fails, it requests the super-types to validate the argument.
<-check
method is the normal way to activate a type. The first argument is the
object to be checked. The second is the receiver that requests the
conversion. The latter is currently used only for the
type<->kind
member.
Types are normally referred to by their type<-name
rather than by their reference. For this reason type checking and
conversion is also accessible through pce<-convert.
The following two lines are identical:
?- get(@pce, convert, '34', int, Int). ?- get(type(int), check, '34', Int).
First, type->validate
is activated. If this fails, type<-translate
is activated and the result thereof is returned.
<Type> ::= <KindType>
| <SingleType> '...' % Methods: Varargs
<KindType> ::= 'alien:'<C-type> % Alien (C) types
| 'member:'<Single> % Members
<Single> ::= <PrimType>
| <Single>'|'<Single> % Disjunction
| '['Single']' % May be @default
| <Single>'*' % May be @nil
<PrimType> ::= <PredefinedType> % any defined type
| {<Name>',' ...} % name_of
| <int>'..'<int> % integer range
| <real>'..'<real> % real range
| <ClassName>
Below are some examples:
point Instance of class point or a subclass
int integer
[name] A name or @default
graphical* A graphical object or @nil
int|point integer or point
{left,right} either the name 'left' or 'right'
|function
Accepts a name or a function
object.
?- get(@pce, convert, '{left,center,right}', type, X),
?- get(X, copy, format_name, _).
Creates a type format_name that accepts the names’left’,’center’or’right’.
translate(Type type, Any value, Object context)
{ Activate translate_function.
IF successful, return the translated value;
ELSE try super-types until one succeeds and return the value
}
<-check
because the argument may not be of the requested type.
<-completions
to perform automatic completion.
Currently this method takes care of:
<-kind
= name_of types ({name1,name2})
<-kind
= int_range if the range entails at most 10 integers
<-kind
= value
<-supers