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