Created
June 25, 2019 03:02
-
-
Save joseoliv/46f84facbb39f892e7d9ba71ce47bdf3 to your computer and use it in GitHub Desktop.
runPastCode
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 | |
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