66 lines
1.3 KiB
Plaintext
66 lines
1.3 KiB
Plaintext
//_d so the test doesn't run yet
|
|
|
|
// capturing closures in lists:
|
|
|
|
var f = funct() {
|
|
var y = 5;
|
|
var x = @[
|
|
funct() print y,
|
|
funct() y = y + 1
|
|
];
|
|
:result = x;
|
|
};
|
|
|
|
//expect:5.0
|
|
f()[0]();
|
|
f()[1]();
|
|
//expect:6.0
|
|
f()[0]();
|
|
|
|
// capturing the result of a function:
|
|
|
|
var f2 = funct() {
|
|
var x = { @f2
|
|
:result = funct() {
|
|
print :f2;
|
|
};
|
|
};
|
|
x = 5;
|
|
};
|
|
|
|
//expect:5.0
|
|
f2()();
|
|
|
|
// oop: constructors, getters, setters
|
|
|
|
var newAnimal = funct(species, color) {
|
|
var species = species; // copy it so that it's mutable, if args ever get immutable
|
|
var animal = @{
|
|
"getSpecies" = funct() :result = species,
|
|
"setSpecies" = funct(newSpecies) species = newSpecies,
|
|
"getColor" = funct() :result = color, // this captures an argument directly
|
|
};
|
|
:result = animal;
|
|
};
|
|
|
|
var horse1 = newAnimal("horse", "brown");
|
|
var horse2 = newAnimal("horse", "white");
|
|
|
|
var turtle = newAnimal("turtle", "brown");
|
|
|
|
//expect:horse
|
|
print horse1["getSpecies"]();
|
|
horse1["setSpecies"]("zebra");
|
|
//expect:zebra
|
|
print horse1["getSpecies"]();
|
|
|
|
//expect:brown
|
|
print horse1["getColor"]();
|
|
//expect:horse
|
|
print horse2["getSpecies"]();
|
|
//expect:white
|
|
print horse2["getColor"]();
|
|
//expect:turtle
|
|
print turtle["getSpecies"]();
|
|
|
|
// TODO: tests for enclosing upvalues multiple levels up |