[Mozart Oz Users] newbie question (declarations)

Peter Van Roy pvr at info.ucl.ac.be
Wed Mar 7 13:10:19 CET 2001


Here is a simple 'vade mecum' for declarations.

Hope it helps,

Peter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The following statements create new variable *identifiers*,
i.e., new variable names that can be used in the program.
Initially, the variable identifiers refer to new unbound
variables in the store.

    local X Y Z in ... end
    -> Declares new identifiers that are available up to
       the matching 'end'.

    declare X Y Z in ...
    -> Declares new identifiers in the interactive
       environment.  Cannot be used inside a program.
       It's like a 'local' where the 'end' is put in
       automatically when the Oz emulator halts.

Syntactic short-cuts:

    -> The 'local' and 'end' can be removed if they are clear
       from the context.  I.e., instead of:

          if 1<2 then  local X in ... end  end

       you can write:

          if 1<2 then X in ... end

    -> The identifiers before the 'in' can be initialized
       right away.  I.e., instead of:

          local 
             Foo Bar
          in 
             Foo=23 
             proc {Bar} skip end
             ...
          end

       you can write:

          local 
             Foo=23 
             proc {Bar} skip end
          in 
             ... 
          end

       This avoids having to write the variable names twice.

As far as I can tell, this completely explains the use of 'in'.
Once you get used to the syntactic short-cuts, you will love
them (I swear!).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The following statements create *data structures*, not
identifiers.  The variable identifier must have been
declared before with 'declare' or 'local'.

    functor F import ... export ... define ... end
    -> Defines a new functor F.  Between 'define' and 'end'
       is like between 'local' and 'in': it creates new
       identifiers and initializes them right away.

    class C ... end
    -> Defines a new class C.

    proc {P ...} ... end
    -> Defines a new procedure P.

    fun {F ...} ... end
    -> Defines a new function F.
       (functions are syntactic sugar for procedures with
       one extra argument)

This is not as weird as you might think at first glance.
It's a natural consequence of the dynamic nature of Oz.
For example, writing X=23 will create a new integer '23'
and bind it to X.  It's the same with functors, classes,
procedures, and functions, which are values just like 23.

Syntactic short-cut:

    All of these can be nested by replacing the name with
    a $ (dollar sign).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-
Please send submissions to users at mozart-oz.org
and administriva mail to users-request at mozart-oz.org.
The Mozart Oz web site is at http://www.mozart-oz.org/.





More information about the mozart-users mailing list