Contact | Privacy | Datenschutzerklärung | Impressum

Methods

Methods can be defined using the def or defenv tag.
Syntax:
heitml::= <def MethodName FormalParameterList > DefinitionModifiers
    Body
</def>
| <defenv MethodName FormalParameterList > DefinitionModifiers
    Body
</defenv>
| ...
Body::= heitml *

Every method belongs to a class. Methods defined inside a Class belong to that class. Top level methods (that are not located inside a class definition) belong to a predefined class named Page.

Calling Top Level Methods

Top level methods can be called using the tag-syntax or using the function call syntax. In both cases a function call executes the body of the def or defenv tag.

Tag Syntax for Method Call

The tag syntax to call a method is
Syntax:
heitml::= BeginTag [ Content EndTag ] | EmptyElementTag
BeginTag::= < MethodName ActualParameterList >
EndTag::= </ MethodName >
EmptyElementTag::= < MethodName ActualParameterList />
Content::= heitml *

This resembles the usual HTML or XML syntax or an element. A method defined with def may be called with a single BeginTag or a EmptyElementTag. A method defined with defenv may be called with a BeginTag and a EndTag or with an EmptyElementTag. Note that the EndTag must not be present in case of a def and it must be present in case of a defenv. To retain HTML compatibility there is the defautoclose declaration that automatically inserts missing EndTags in certain cases.

One can choose between HTML compatible and heitml extended parameter lists.

Example:

input:resulting output:
<def Hello>
Hello World
</def>
<Hello/>

Hello World

A method defined with the def-tag must be called without the content and the end tag. A method defined with the defenv-tag must be called with content and end tag. The identifier of the end tag must match the identifier of the start tag.

Example:

input:resulting output:
<defenv Big
><font size="+3"><defbody></font><
/defenv>
<big>Hallo</big>
Hallo

Visibility

A method can be called only after it has been defined. In this case, the word "after" refers to the sequence of text in the input file.

Methods can be redefined; however, the type of method (def or defenv) must stay the same. Always the textually last definition is taken. This way it is possible to use an initial empty definition for a method as a kind of forward declaration.

Methods can also be called recursively. If two methods call each other recursively, it is necessary to define one first as empty, and afterwards redefine it.

Example:

input:resulting output:
<def Hello></def>
<Hello/>
<def Hello>Hello World</def>
<Hello/>
Hello World

Hello World

Function Call

Syntax:
Expr::= FunctionName ( ActualParameterList )
The function call syntax can be used to call top level methods and built-in functions. The method must be defined using the def-tag and it must contain a return tag. A function call is an expression. During evaluation the function is called. The result of the expression is the value returned with the return tag.

Example:

input:resulting output:
<def plus a b; return a+b; /def>
<? plus(5,7)>
<? plus("Hello ", "World")>
12
Hello World

Local Variables

Every method has its own set of Local Variables. They are visible and can be access only within this single method. Variables of the calling method or called methods can not be accessed. If methods call each other recursively, multiple incarnations of the local variables exist.

In contrast the variables ff, se, page, and gl are Global Variables and can be accessed everywhere. These are objects and can therefore contain as many global fields as desired.

Example:

input:resulting output:
<def myfun;
 a=1; gl.b=2;><
/def>

<let a=5; gl.b=6>
<myfun/>
<?a> <?gl.b>



5 2

Parameter Passing

Methods can have parameters. Using the formal parameter list at the definition parameters can be specified. These parameters can then be passed by a method call. A def or defenv tag can have a formal parameter list of the form
Syntax:
FormalParameterlist::= ( ParameterName [= Expr ]  | * ParameterName  | ** ParameterName
      | @ VariableName  | @ this
  ) * [ ... ] .

Parameters marked with an asterisk * are Output Parameters. Parameters marked with ** are Id Parameters. All other parameters are Input Parameters. The optional Expression associated with a parameter is called Default Value. The variable given after a @ is named target variable.

Now please consider a method call with an ActualParameterList
Syntax:
ActualParameterlist::= ( [ ParameterName = ] Expr ) * .

All parameters with the optional "Identifier =" in front are called Keyword Parameters. Parameters without the "Identifier =" are called Positional Parameters.

During the call every Actual Parameter is associated with a Formal Parameter: The actual parameter in the n-th position is associated with the n-th formal parameter. A Keyword Parameter is associated with the Formal Parameter having the same name.

It is a bug if there are two actual keyword parameters with the same name or if a Keyword Parameter and a positional parameter are associated with the same formal parameter. If a formal parameter has no actual parameters associated with it, then the Default Value is used instead. If no default value is given the parameter must be specified by a call. The called method or function gets a local variable for each formal input parameter containing the value of the associated actual parameter.

Example:

input:resulting output:
<def mytag a b=5 c=9;
   ? a>, <? b>, <? c><
/def>

<mytag 1 2 3 />
<mytag 7 />
<mytag c=11 7 />
<mytag a=5 c=11 />
<mytag (1,2,3); mytag(a=5 c=11) /> 

1, 2, 3
7, 5, 9
7, 5, 11
5, 5, 11
1, 2, 35, 5, 11 

For Output Parameters the Actual Parameter has to denote a variable or an object field. When the called method returns, the Formal Parameter is assigned to the Actual one. (i.e. a Local Variable of the method with the name of the Formal Parameter)

Example:

input:resulting output:
<def mytag *a b=5 c=9;
   let a = b + c;
/def>
<mytag a 2 3 /> <?a> <br>
<mytag a /> <?a>

 5 
14

Using the @ syntax, parameter values can be stored in an object, rather than passing them as local variables. All parameter values that occur after the @ are put into an object. It can be referenced by the variable name following the @. Access to an actual parameter is done by referencing the object field with the name of the corresponding formal parameter.

If the last parameter is followed by ..., parameters may be passed for which no formal parameter exists (anonymous parameters). This is very useful in tag redefinitions, that only handle a subset of the parameters of the original definition. Using @, the anonymous parameters may be processed by looking for object fields with a name that does not occur in the formal parameter list.

Example:

input:resulting output:
<def myimg @p ...;
 p.src="/heitml2.1/pictures/" + p.src
><\img <? p "Pn">><
/def>
<myimg src="heitmlsm.jpg" width=50 height=50 />


Only a class constructor may have an id parameter (marked with **). The actual parameter must either denote a variable or evaluate into a string s. In the latter case, the variable page[s] is taken. If the variable is not defined or contains null, the object created by the constructor is assigned to the variable upon creation. If the variable exists and has a value other than null, it must contain an object. Instead of creating a new object the constructor uses this object to work on. The object becomes accessible by this inside the constructor. Unlike other parameters the parameter value is accessible by this and not by the parameter name specified. As an exemption to this rule a string value passed to an id parameter is accessible by the parameter name.

Example:

input:resulting output:
<def cnt **oid; inherit Object;
    this.i=default(this.i,0)+1;
    ? this.i;
/def>
<cnt a />, <cnt "b" />, <cnt a />, <cnt a />, <cnt "b" /> <br >
<? a "n"><br >
<? page.b "n">
1, 1, 2, 3, 2 
a=cnt(i=3)

b=cnt(i=2)


This page was dynamically generated by the web application development tool RADpage of H.E.I. See "Was ist AJAX? " (in German). In Germany H.E.I. provides Webdesign in Mannheim and Web Programming (Programmierung).
© 1996-2024 H.E.I. All Rights Reserved.



Homepage
Intro/Features
Component Guide
Programming
  Language Guide
  Language Ref.
    General Design
    Lexical Structure
    Expressions
    Objects
    Methods
    Classes
    heitml Tags
    heitml Functions
    Advanced Functions
    Database Access
    Global Variables
    Form Fields
    Server Variables
    Sessions
    heitml Syntax
  Component Ref.
  Class Library
  User Components
  Tutorial
  New Features
  heitml 1
User Guide
Services
Privacy
Datenschutz
 
Contact / Impressum