Παρασκευή, 22 Ιουνίου 2018

Module decoration and passing variables by name

In M2000 we can call modules (procedures with inner modules/functions/subs) using name only, or using Call and name. Using Call we can call itself. Using name only, we can't call itself but we can use decoration. A decoration is a mechanism to place modules inside a module before the calling. Each time we call a module, interpreter just get the code from a list and execute it. If a module placed as decoration in a module say Kappa then at execution of Kappa a module definition for this decoration skipped. 

In the example bellow we see that we can pass values using names, in any order. So in module Alfa we can use 1 and 2 to feed by value x and z, or we can use %z=1, %x=2 to feed z and x before calling Alfa, but these are local to Alfa.


Module Alfa (x, z) {
      Print "x=";x
      Print "z=";z
}

Alfa 1, 2
\\ By Name pass
Alfa %z=1, %x=2

Module Alfa (x as Integer, z as Double) {
      Print "x=";x,"  "+ Type$(x)
      Print "z=";z, "  "+Type$(z)
}

Alfa 1, 2
\\ By Name pass
Alfa %x=2, %z=1

Module Kappa {
      Module Beta {
            Print Stack.Size ' 2
            Drop 2
      }
      Beta 1, 2
      Beta %z=1, %x=2
}
Kappa
\\ Passing temporary module in place of other module
Print "Decorate Kappa using Beta as Alfa"
Kappa ; Beta as Alfa
Module Zeta (x , z) {
      Print "x=";x,"  "+ Type$(x)
      Print "z=";z, "  "+Type$(z)
}
Kappa ; Beta as Zeta
Kappa