slint/internal/compiler/tests/syntax/functions/functions_purity.slint
Aurindam Jana 0cfeec1a31
Update Slint Community License (#4994)
Updated the version from 1.1 to 1.2 
Renamed the header to "Slint Royalty-free Desktop, Mobile, and Web Applications License"
Added definition of "Mobile Application" and grant of right
Moved "Limitations" to 3rd section and "License Conditions - Attributions" to 2nd section
Added flexibility to choose between showing "MadeWithSlint" as a dialog/splash screen or on a public webpage
Moved the para on copyright notices to section under "Limitations"
2024-04-15 15:18:55 +02:00

162 lines
3.3 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.2 OR LicenseRef-Slint-commercial
export component Foo {
property <int> prop;
callback c1;
pure callback c2;
function f1() {
prop = 1;
}
function f2() -> int { 42 }
pure function f3() {
f2();
f1();
// ^error{Call of impure function 'f1'}
prop /= 5;
// ^error{Assignment in a pure context}
}
public function f4() {}
pure function f5() {
f1();
// ^error{Call of impure function 'f1'}
f2(); // ok, private function auto-detected as pure
f3();
f4();
// ^error{Call of impure function 'f4'}
c1();
// ^error{Call of impure callback 'c1'}
c2();
}
protected function f6() {}
c1 => { f2() }
c2 => {
c1();
// ^error{Call of impure callback 'c1'}
}
property <int> p1: f2();
property <int> p2: {
p1 = 42;
// ^error{Assignment in a pure context}
42
};
property <int> p3: {
pw.show();
// ^error{Call of impure function}
fs.focus();
// ^error{Call of impure function}
f6();
// ^error{Call of impure function 'f6'}
42
};
pw := PopupWindow {}
fs := FocusScope {}
callback model() -> [int];
for xx in model() : Rectangle {
// ^error{Call of impure callback 'model'}
property <int> abc: xx;
}
}
export component Bar {
pure callback xc1 <=> f.c1;
// ^error{Purity of callbacks 'xc1' and 'f.c1' doesn't match}
callback xc2 <=> f.c2;
// ^error{Purity of callbacks 'xc2' and 'f.c2' doesn't match}
f := Foo {
c2 => {
self.c1();
// ^error{Call of impure callback 'c1'}
}
}
}
export Foo_Legacy := Rectangle {
property <int> prop;
callback c1;
pure callback c2;
function f1() {
prop = 1;
}
function f2() -> int { 42 }
pure function f3() {
f2();
f1();
// ^warning{Call of impure function 'f1'}
prop /= 5;
// ^warning{Assignment in a pure context}
}
public function f4() {}
pure function f5() {
f1();
// ^warning{Call of impure function 'f1'}
f2(); // ok, private function auto-detected as pure
f3();
f4();
// ^warning{Call of impure function 'f4'}
c1();
// ^warning{Call of impure callback 'c1'}
c2();
}
c1 => { f2() }
c2 => {
c1();
// ^warning{Call of impure callback 'c1'}
}
property <int> p1: f2();
property <int> p2: {
p1 = 42;
// ^warning{Assignment in a pure context}
42
};
property <int> p3: {
pw.show();
// ^warning{Call of impure function}
fs.focus();
// ^warning{Call of impure function}
42
};
pw := PopupWindow {}
fs := FocusScope {}
}
export Bar_Legacy := Rectangle {
pure callback xc1 <=> f.c1;
// ^error{Purity of callbacks 'xc1' and 'f.c1' doesn't match}
callback xc2 <=> f.c2;
// ^error{Purity of callbacks 'xc2' and 'f.c2' doesn't match}
f := Foo {
c2 => {
self.c1();
// ^warning{Call of impure callback 'c1'}
}
}
}