Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Last active March 25, 2020 12:43
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/7eb42ac948df870c1ffd068b705c59ff to your computer and use it in GitHub Desktop.
Save joseoliv/7eb42ac948df870c1ffd068b705c59ff to your computer and use it in GitHub Desktop.
replaceCallBy
package metaobjectTest
@doc{*
Annotation 'replaceCallBy' should be attached to a final method. It
replaces any message passings to the method by the attached DSL code.
The DSL code may use the parameters, which are re-calculated if
parameter 'once' to the annotation is not used.
All methods of the prototype ReplaceCallBy are final because it is a
final prototype.
*}
object ReplaceCallBy
func run {
var value = threeTimes: 3;
assert value == 9;
value = threeTimes: next;
assert value == 3;
nextValue = 0;
value = threeTimesOnce: 3;
assert value == 9;
nextValue = 0;
// value = 1 + 1 + 1
value = threeTimesOnce: next;
assert value == 3;
}
@replaceCallBy{* 3*n *}
func threeTimes: Int n -> Int = n;
@replaceCallBy(once){* n + n + n *}
func threeTimesOnce: Int n -> Int = n;
func next -> Int { ++nextValue; return nextValue; }
var Int nextValue = 0;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment