Gentee Programming Language > Documentation > Syntax | Download documentation |
You can define different methods for the structured types. Any method is a function associated with an object of the appropriate type, that the method should operate on.
A method is defined by specifying the keyword method followed by the name of the return type (if it is required), an object type and a method name followed by a separating period. Like declaring a function, you should specify method parameters followed by its body.
<method declaration> ::= method [<type name>] <type name>'.'<method name> ['(' [<parameters declaration>] ')']
<method> ::= <method declaration><function body>
Actually, method parameters are declared like function parameters. The parameter this is created automatically within the method; furthermore, this parameter contains the object to which the given method is called. The parameter this has the same type as the object does.
Methods are responsible for object initialization and destruction, getting index and type conversion as well as for other operations. Moreover, such methods as: initialization, destruction, getting index must have their own fixed names:
init Initialization. This method has no parameters; furthermore, it is called automatically with its variable defined or with its object created using the function new. delete Deletion. This method has no parameters; furthermore, it is called automatically if the variable is destroyed when the execution of a function or a block is completed, or if the object that contains objects of the appropriate type is deleted. index Getting an index, this method serves the following purposes: for example, it specifies either the i-character of a string or the i-element of one-dimensional array. Uint type is the required parameter. In order to call this method, you should specify it with the index enclosed in square brackets. array This method is used to define the size of the array. The method has only one uint parameter that contains the number of dimensions of the array. It is applied for declaring multidimensional arrays. eof Tests for the end of a list. This method has no parameters and is applied using the foreach operator. first Returns the first element from a list. This method has no parameters and is applied using the foreach operator. next Returns the next element from a list. This method has no parameters and is applied using the foreach operator.
method uint str.index( uint i )
{
return this.sbuf.data + i
}
...
str mystr = "This is a string"
a = mystr[3] // = 'i'
Type conversion is also declared with the help of the methods. A source type is specified as the object type of the method and a destination type of the object is specified as the method name. If the destination type is structured, the result object must be specified in the parameter.
//The method converts uint number to string representation
method str uint.str( str result )
{
result.setlen( 0 )
int2str( result, "%u", this )
return result
}
...
str mystr
uint a = 10
mystr = str( a ) //Type conversion
See also
![]() |
![]() Copyright © 2004-2006 Gentee Inc. All rights reserved. ![]() |