aboutsummaryrefslogtreecommitdiff
path: root/examples/own.fwd
blob: 66348865548f3aa28d3698c514bba4408dc03c5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
do_something((auto) a);
consume(auto a);

main()
{
	do_something() => auto a;
	do_something() => auto b;
	!> e {
		/* if do_something() fails, a is still alive, so we must kill
		 * it. However, we might enter this error block after the
		 * callback to do_something() was run, and a might've already
		 * been moved. own just checks if we still own the resource and
		 * executes the block.
		 *
		 * admittedly, purely syntactically it would look like b is
		 * already alive at this point, but since it's actually within
		 * the implicit closure, it is not. Might play around with ordering
		 * the error block to come before implicit closures...
		 */
		own a {consume(a);}
		error e
	}

	consume(a);
	!> e {
		/* if consume fails, b is still alive, but either this error
		 * block runs (and throws an error) or the next consume is run,
		 * so own is not necessary here */
		consume(b);
		error e
	}
	consume(b);
}