Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Metaobject changeFunctionForMethod
package metaobjectTest
@doc{*
This metaobject should only be attached to methods 'functionForMethod:1' and
'functionForMethodWithSelf:1' of Any. It replaces method calls to these
methods by an anonymous function. If the parameter of 'functionForMethod:1' is
a literal string "m:1", then the code that
replaces the method call is
{ (: T elem :) receiver m: elem }
T is the type of the parameter of method "m:1", which should be
func m: T p { ... }
*}
object ChangeFunctionForMethod
func run {
var typeof(self) p = ChangeFunctionForMethod();
assert (p functionForMethod: "zero") eval == 0;
assert (p functionForMethod: "addOne:1") eval: 4 == 5;
assert (p functionForMethodWithSelf: "zero") eval: p == 0;
assert (p functionForMethodWithSelf: "addOne:1") eval: p eval: 4 == 5;
}
func zero -> Int = 0;
func addOne: Int n -> Int = n + 1;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment