Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 {
var Int s = 0;
for elem in self {
s = s + elem getValue;
}
return s
}
*}
object AddCodeFromMetaobject
// methods getValue and setValue: will be added to prototype
@property Int value = 0;
@init(value)
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