Last active
August 30, 2022 08:15
-
-
Save joseoliv/aa3a096c8b18f33410d2b0e1208a9321 to your computer and use it in GitHub Desktop.
Metaobjects addCodeToGenericPrototype and AddCodeFromMetaobject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package metaobjectTest | |
/* | |
@addCodeFromMetaobject is in prototype Array. The annotation | |
addCodeToGenericPrototype gives the code to be added to the | |
array of prototype AddCodeFromMetaobject. | |
*/ | |
@addCodeToGenericPrototype("Array", " func sumAll -> Int"){* | |
func sumAll -> Int { | |
var Int s = 0; | |
for elem in self { | |
s = s + elem getValue; | |
} | |
return s | |
} | |
*} | |
@init(value) | |
object AddCodeFromMetaobject | |
// methods getValue and setValue: will be added to prototype | |
@property Int value = 0; | |
func init { | |
} | |
func run { | |
var array = [ | |
AddCodeFromMetaobject(1), AddCodeFromMetaobject(2), | |
AddCodeFromMetaobject(3) ]; | |
/* method sumAll will be added to Array<AddCodeFromMetaobject> by | |
metaobject addCodeFromMetaobject. This method was passed by | |
annotation addCodeToGenericPrototype of this prototype | |
(AddCodeFromMetaobject) | |
*/ | |
assert array sumAll == 6; | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment