Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
runPastCode
package metaobjectTest
import cyan.io
@doc{*
Annotation 'runPastCode'
*}
object RunPastCode
func run {
self testImportProgram;
self testPastCode;
}
func fat: Int n -> Int {
if n == 0 { return 1; } else { return n*(fat: n - 1); }
}
func testImportProgram {
var String color = favoriteColor;
var RunPastCode past = self;
var Int f5 = 0;
@runPastCode(false, past){*
var Int fatorial5 = past fat: 5;
return "f5 = " ++ fatorial5 ++ ";";
*}
if f5 == 120 {
"runPastCode was called with 'true'" println
}
else {
"runPastCode was called with 'false'" println
}
f5 = 0;
var RunPastCode past2 = self;
@runPastCode(true, past2){*
var Int fatorial5 = past2 fat: 5;
return "f5 = " ++ fatorial5 ++ ";";
*}
if f5 == 120 {
"runPastCode was called with 'true'" println
}
else {
"runPastCode was called with 'false'" println
}
}
func testPastCode {
var RunPastCode past3 = self;
favoriteColor = "blue";
@runPastCode(true, past3){*
return " favoriteColor = \"" ++
past3 getFavoriteColor ++ "\";";
*}
/* - change the assignment
favoriteColor = "blue";
to
favoriteColor = "cyan";
compile this prototype with the first
parameter to the annotation above set to 'false'
- change the assignment
favoriteColor = "cyan";
to
favoriteColor = "blue";
uncomment the next line and compile this prototype
with the first parameter to the annotation above set to 'true'
The annotation will generate an assignment to 'favoriteColor'
using the previous value of 'favoriteColor', which is 'cyan'
*/
assert favoriteColor == "cyan";
printexpr favoriteColor;
}
@property var String favoriteColor = "blue";
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.