Aztec® Programming Language
Version 1.1 Alpha 2

Copyright © 2010-2017, Aztec Development Group, All Rights Reserved

Download Aztec

Search        Contact Us

I can't remember if I cried...

When I read about his widowed bride.

But something touched me deep inside...

The day... the music died.

- Don McLean

 

An Aztec data item may be defined as a reference to a method. Once a method reference is set with a valid method, the method reference can be used to execute the method which it references.

♦ The 'method' keyword is embedded inside the data type to indicate a method reference. The data types for the method argument(s) are embedded inside method<> brackets and the method return type is inside the data<> brackets

♦ e.g. data<int method<float,string>> MyMethodRef # Reference to a method that takes float and string args, and returns int

♦ When the method reference is set, it can point at an instance method, a global or a shared method.

♦ This allows the same Aztec code to call a different method based on the current value of the method reference.

♦ When the method reference points at an instance method, the reference to the instance object is internally stored.

♦ When the method reference is used to actually execute the method, the instance object is automatically pushed onto the stack prior to the method call.

♦ Method references are completely type safe. The compiler and VM will only allow a method reference to be set with a method which completely matches the argument list and the method return type.

♦ Method references can be passed as arguments to a method.

♦ A method reference variable can always be passed as an argument.

♦ A real method name can only be passed as a method argument if the ‘unique’ keyword is specified in the method definition.

♦ Data items which are defined as method references are initialized to “null” by default.

♦ Method references are particularly useful for event handling, and all Aztec Events support registering event handlers using method references.

♦ A special data type, referred to as a "base method reference" also exists. It is somewhat similar in concept to the Base class for standard Aztec objects.

♦ The syntax "base method" is used to denote a base method reference.

♦ Normally, a method reference data item must have the exact same "signature" (method return value and method argument list) as the method it references.

♦ The base method reference provides a "generic" storage item for any type of method reference.

♦ The generic method reference storage concept makes the base method reference very useful in a method argument list so the user can pass in any type of method reference.

♦ The base method reference can not be executed like a standard method reference, since the compiler does not know ahead of time what type of method reference it might hold.

♦ The base method reference can be "evaluated" or "converted" to a standard method reference using the 'is' and 'as' keywords, similar to how they are used with object type testing and conversion.

♦ The 'is' operator will return true if the base method reference matches the type of method reference being tested.
♦ The 'as' operator will return the actual method reference if the base method reference matches the type of method reference being tested, and returns null if it does not match.

♦ Some example method reference syntax with descriptions below

Aztec Method Reference Syntax Examples

 

♦ MethodRef1 is a method reference to a method which has no arguments and which does not return a value.

♦ MethodRef2 is a method reference to a method which has a single string argument and returns an int.

♦ MethodRef3 is a method reference to a method which has a Thread argument followed by an int argument, and the method returns a string.

♦ MethodRef4 is a base method reference. The "base method" syntax must be used as is, with no method arguments.

♦ The Type Statement is oftentimes used along with a method reference to create a simple Data Type name to represent a particular method reference.

♦ The new data type name is much easier to use in practice, and also ensures consistency when using a specific method reference definition.

♦ This approach must be used when defining a method reference array and when using a method reference as a method argument. The 'method' keyword is currently not supported directly in those situations.

♦ An example script showing the use of method references

#===================================================================================================
# Example Script: Aztec Method References Script
# Demonstrates use of simple Aztec method references outside of event handling, since method refs
# used in event handling are shown in many other code examples on the website.
#===================================================================================================

type<method<int,string>> MethRefIntStringArgs
type<string method<float>> MethRefFloatArg

# Several methods used for demonstration purposes. They will be called via method references.
method unique NormalMethod1(int Arg1, string Arg2)
{
StdIO.Write("Inside Normal Method1 - int is " + Arg1.Str() + " and string is '" + Arg2 + "'")
}

method<string> NormalMethod2(float Arg1)
{
StdIO.Write("Inside Normal Method2 - float is " + Arg1.Str())
return(Arg1.Str())
}

# Main run-time entry point for the script.
method Main
{
data<MethRefIntStringArgs> MethRefIntStr
data<MethRefFloatArg> MethRefFloat

# Set each method ref with an actual method and then execute the method via references.
MethRefIntStr = NormalMethod1
MethRefFloat = NormalMethod2

MethRefIntStr(1,'lmnop')
MethRefFloat(float.Pi)

# Pass a method ref (real method) as an argument and let it execute it.
ExecuteIntStringMethodRef(NormalMethod1)
}

# Method takes a method reference as an argument and executes it.
method ExecuteIntStringMethodRef(MethRefIntStringArgs MethodRef)
{
# Execute the method via the reference and pass it an int and a string.
MethodRef(101,'Aztec')
}

Page UpPage DownCopyright © 2010-2017
Aztec Development Group
All Rights Reserved

Download Aztec