Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Last active August 30, 2022 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseoliv/aa3a096c8b18f33410d2b0e1208a9321 to your computer and use it in GitHub Desktop.
Save joseoliv/aa3a096c8b18f33410d2b0e1208a9321 to your computer and use it in GitHub Desktop.
Metaobjects addCodeToGenericPrototype and AddCodeFromMetaobject
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