`with` across function calls
Simen Kjærås
simen.kjaras at gmail.com
Fri Jan 18 14:29:59 UTC 2019
On Friday, 18 January 2019 at 12:35:33 UTC, Nicholas Wilson wrote:
> void bar(with Global g)
> {
>
> }
How does this work with nested calls? e.g.
struct Global {
int n;
}
void foo() {
auto g = Global(0);
with (g) {
bar();
}
}
void bar(with Global g) {
assert(g.n == 0);
baz();
assert(g.n == 0); // g is not overwritten by baz's new g
}
void baz() {
auto g = Global(1);
with (g) {
qux();
}
}
void qux(with Global g) {
assert(g.n == 1); // use the nested g
}
If my intuition is correct, the above should compile with no
asserts triggered.
I further expect that attempting to call bar() or qux() outside a
with(g) will fail:
unittest {
bar(); // Error: calling bar requires Global g in surrounding
with() scope.
Global g;
bar(); // Error: Global g found in surrounding scope, but not
used in with().
}
--
Simen
More information about the Digitalmars-d
mailing list