Recital

Login Register

Using anonymous classes in Recital

In Recital 10, you can declare anonymous classes and call anonymous methods in these classes.
// declare some simple procedures 
proc display(cArg)
    echo "display=" + cArg
endproc

proc show(cArg)
    echo "show=" + cArg
endproc

// create an object based on an anonymous class
myobj = new object()

// add some properties
myobj["name"] = "barry"
myobj["company"] = "recital"

// now declare an anonymous method
myobj["mymethod"] = display

// call the method
myobj.mymethod("hello world")    // displays "display=hello world"

// redeclare the method
myobj["mymethod"] = show

// call the method
myobj.mymethod("hello world")    // displays "show=hello world"
Where this becomes particularly useful is when you have a procedure that calls anonymous methods in order to process data. This technique can be used to call anonymous procedures in your code.
proc processdata(oArg)
    oArg.mymethod(oArg.name)    
endproc

proc show(cArg)
    echo "show=" + cArg
endproc

myobj = new object()
myobj["name"] = "barry"
myobj["mymethod"] = show
processdata(myobj)        // displays "show=barry"
Twitter

Copyright © 2024 Recital Software Inc.

Login

Register

User Registration
or Cancel