Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Last active June 22, 2018 18:09
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/af1896ffde90eefcd15fecc0ec058b69 to your computer and use it in GitHub Desktop.
Save joseoliv/af1896ffde90eefcd15fecc0ec058b69 to your computer and use it in GitHub Desktop.
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