2021-02-28 14:10:17 +01:00
|
|
|
//[Test: shadowing]
|
|
|
|
//[source: mixed]
|
2021-01-10 19:32:28 +01:00
|
|
|
//similar to vars.jpl, but more focused on shadowing
|
|
|
|
|
|
|
|
// simple shadowing
|
|
|
|
var x = 4;
|
|
|
|
{
|
|
|
|
var x = 5;
|
2021-01-30 18:42:04 +01:00
|
|
|
print(x);//stdout:5
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
print(x);//stdout:4
|
2021-01-10 19:32:28 +01:00
|
|
|
|
|
|
|
// type changing shadowing
|
|
|
|
var y = true;
|
|
|
|
{
|
|
|
|
var y = 2;
|
2021-01-30 18:42:04 +01:00
|
|
|
print(y);//stdout:2
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
print(y);//stdout:true
|
2021-01-10 19:32:28 +01:00
|
|
|
|
|
|
|
// no shadowing here
|
|
|
|
var z = 3;
|
|
|
|
{
|
|
|
|
z = true;
|
2021-01-30 18:42:04 +01:00
|
|
|
print(z);//stdout:true
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
print(z);//stdout:true
|
2021-01-10 19:32:28 +01:00
|
|
|
|
|
|
|
//in-function shadowing
|
|
|
|
fun shadow(x) {
|
|
|
|
//will be called once with the input 3
|
2021-01-30 18:42:04 +01:00
|
|
|
print(x);//stdout:3
|
2021-01-10 19:32:28 +01:00
|
|
|
{
|
|
|
|
var x = 4;
|
2021-01-30 18:42:04 +01:00
|
|
|
print(x);//stdout:4
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
print(x);//stdout:3
|
2021-01-10 19:32:28 +01:00
|
|
|
x = nil;
|
2021-01-30 18:42:04 +01:00
|
|
|
print(x);//stdout:nil
|
2021-01-10 19:32:28 +01:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2021-01-30 18:42:04 +01:00
|
|
|
print(shadow(3));//stdout:nil
|
2021-01-10 19:32:28 +01:00
|
|
|
|
|
|
|
//shadowing functions
|
|
|
|
fun hello() {
|
|
|
|
print("hello");
|
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
hello();//stdout:hello
|
2021-01-10 19:32:28 +01:00
|
|
|
{
|
|
|
|
fun hello() {
|
|
|
|
print("hello in");
|
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
hello();//stdout:hello in
|
2021-01-10 19:32:28 +01:00
|
|
|
{
|
|
|
|
fun hello() {
|
|
|
|
print("hello inmost");
|
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
hello();//stdout:hello inmost
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
hello();//stdout:hello in
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
hello();//stdout:hello
|
2021-01-10 19:32:28 +01:00
|
|
|
|
|
|
|
//functions shadowing with type change
|
|
|
|
fun eat() {
|
|
|
|
print("nom nom nom");
|
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
eat();//stdout:nom nom nom
|
2021-01-10 19:32:28 +01:00
|
|
|
{
|
|
|
|
var eat = 4;
|
2021-01-30 18:42:04 +01:00
|
|
|
print(eat);//stdout:4
|
2021-01-10 19:32:28 +01:00
|
|
|
{{{{{
|
|
|
|
eat = 5;
|
|
|
|
}}}}} //multiple scopes haha
|
2021-01-30 18:42:04 +01:00
|
|
|
print(eat);//stdout:5
|
2021-01-10 19:32:28 +01:00
|
|
|
}
|
2021-01-30 18:42:04 +01:00
|
|
|
eat();//stdout:nom nom nom
|
2021-02-28 14:10:17 +01:00
|
|
|
//[end]
|
|
|
|
//[end]
|