UIEvolutin Inc.
UJML Language Reference
Function Scoping and Sharing

Functions are scoped identifiers and may be shared.

Functions are identifiers. See Identifiers, User Defined Functions. This means they are available to UJML code according to the scoping rules. See Scope. It also means they may be shared between modules. See Sharing.

Function scope is context dependent

Functions are scoped to the context where they are declared. See Scope. If a function is declared in a module, it has module-level scope. If a function is declared in a state machine, it is scoped to that state machine and any child state machines it contains. 

Variables and parameters declared inside of a function are locally-scoped to the function. See Data Scoping and Sharing.

Public functions in state machines

Functions contained by state machines may be declared as public or private using the visibility modifier. See Sharing. Public functions of a state machine may be used by UJML code which has the state machine in its scope using 'dot notation'. See Identifiers.

Sharing functions between linked modules

Module-level scoped functions may be shared by declaring them as exported or imported using the access modifier. See Sharing. This works by exporting them from a parent module and importing them into a child module which the parent has linked to. See Linking Files

An exported shared function contains parameters, variables, script, and return elements and is the actual implementation of that function. See User Defined Functions, Scripting. An imported function contains only the parameters element and acts as a function 'prototype' for the local module. The declared parameters of an imported function must exactly match the declared parameters of the exported function.

Sharing functions from components

Public functions in components must implement a method of an interface. See Components, Interfaces. You call a component function by casting the component instance reference to a variable of the appropriate interface type and specifying the function using 'dot notation'. See Identifiers, Interface Types.

The following example is a public function of a state machine. It is part of the scrollbar.ujms sample.

<function name="show" type="void" visibility="public">
    <script>
        sScrollThumb = true;
        sScrollBar = true;
        sScrollButton[&BUTTON_UP;] = true;
        sScrollButton[&BUTTON_DOWN;] = true;
    </script>
</function>

 

The following example shows how to call the public state machine function declared above. It is part of the scrolltextbox.ujms sample.

VerticalScrollBar.show();

 

The following example shows how to export a function from an application file. It is part of the visualelements.ujml sample.

<function name="addMenuItem" type="void" access="export">
    <parameters>
        <var name="menuItem" type="string"/>
    </parameters>
    <script>ScrollingMenu.addItem(menuItem);</script>
</function>

 

The following example shows how to import the function exported above into a linked partition file. It is part of the velocale.ujml sample.

<function name="addMenuItem" type="void" access="import">
    <parameters>
        <var name="menuItem" type="string"/>
    </parameters>
    <!-- No script. -->
</function>

 

The following example implements a method of the ILifeBlock interface. It is part of the lifeblock.ujml sample.

<!--
    Sets the alive state of the block.

    Note: The new value of 'alive' will not be available
    until the update() function is called. Until then
    getAlive() will return the previous value.
-->
<function name="ILifeBlock.setAlive" type="void">
    <parameters>
        <var name="value" type="boolean"/>
    </parameters>
    <script>
        mAlive = value;
    </script>
</function>

 

The following example shows how to call the component function declared above from a component instance stored in an array element. It is part of the lifegrid.ujml sample.

mBlocks[rowCtr][colCtr].setAlive(false);
Copyright (c) 2000-2007 UIEvolution, Inc. All rights reserved.
What do you think about this topic? Send feedback!