Add a variable to a class instance at runtime: Difference between revisions

(→‎LOLCODE: Add demonstration)
Line 343:
 
dict[ 'newVar' ] = "I'm Rich In Data"</lang>
 
=={{header|FBSL}}==
FBSL class instances aren't expandable with additional, directly accessible public methods at runtime once the class template is defined in the user code. But FBSL has an extremely powerful feature -- an ExecLine() function -- which permits the user to execute any additional code on the fly either privately (bypassing the main code flow) or publicly (interacting with the main code). ExecLine() can be used for a variety of applications from fine-tuning the current tasks to designing application plug-ins or completely standalone code debuggers. The following class instance may be stuffed up at runtime with any code from simple variables to executable private methods and properties.
<lang qbasic>#APPTYPE CONSOLE
 
CLASS Growable
PRIVATE:
DIM instruction AS STRING = "Sleep(1)"
:ExecCode
DIM dummy = EXECLINE(instruction, 1)
PUBLIC:
METHOD Absorb($code)
instruction = code
GOTO ExecCode
END METHOD
METHOD Yield() AS VARIANT
RETURN result
END METHOD
END CLASS
 
DIM Sponge AS NEW Growable()
 
Sponge.Absorb("DIM b AS VARIANT = 1234567890: DIM result AS VARIANT = b")
PRINT Sponge.Yield()
Sponge.Absorb("b = ""Hello world!"": result = b")
PRINT Sponge.Yield()
 
PAUSE</lang>
'''Output:'''
1234567890
Hello world!
Press any key to continue...
 
=={{header|Groovy}}==