1    type List<T> @abstract =
2        let list = StdList<T>.new
3        let mut is_invoking_callbacks = false
4        let on_added_callbacks = StdList<u32 * T -> Unit>.new
5        let on_removed_callbacks = StdList<u32 * T -> Unit>.new
6        let tokens = StdList<Token>.new
7    
8        let invoke_callbacks (callbacks : StdList<u32 * T -> Unit>
9                              i : u32
10                             x : T) =
11           assert not is_invoking_callbacks
12           is_invoking_callbacks = true
13   
14           for f in callbacks do
15               f i x
16   
17           is_invoking_callbacks = false
18   
19       def is_empty = list.is_empty
20       def is_not_empty = list.is_not_empty
21       def size = list.size
22       def as_slice = list.as_slice
23       def as_safe_slice = list.as_safe_slice
24       def index_of (x : T) = list.index_of x
25       def contains (x : T) = list.contains x
26       def last = list.last
27   
28       fun get (i : u32) = list[i]
29       fun get (i : i32) = list[i]
30   
31       def get_iterator = list.get_iterator
32   
33       def push_token (token : Token) =
34           tokens.add token
35   
36       fun destruct =
37           for i = tokens.size - 1 downto 0 do
38               tokens[i].discard
39   
40       def subscribe (f : u32 * T -> Unit) (g : u32 * T -> Unit) =
41           assert not is_invoking_callbacks
42   
43           on_added_callbacks.add f
44           on_removed_callbacks.add g
45   
46           Token { assert not is_invoking_callbacks
47                   on_added_callbacks.remove f
48                   on_removed_callbacks.remove g }
49   
50       def bind (f : u32 * T -> Unit) (g : u32 * T -> Unit) =
51           let token = subscribe f g
52           for i = 0 until list.size do
53               let x = list[i]
54               f i x
55   
56           Token { token.discard
57                   if list.is_not_empty then
58                       for i = list.size - 1 downto 0 do
59                           let x = list[i]
60                           g i x }
61   
62       def try_single = list.try_single
63   
64   type MutList<T> @[mut_of List] =
65       inherit List<T>
66   
67       def add (i : u32) (x : T) =
68           assert not is_invoking_callbacks
69   
70           list.add i x
71           invoke_callbacks on_added_callbacks i x
72   
73       def add (x : T) = add list.size x
74   
75       def remove_at (index : u32) =
76           assert not is_invoking_callbacks
77   
78           let item = list[index]
79           list.remove_at index
80           invoke_callbacks on_removed_callbacks index item
81   
82       def remove (x : T) =
83           let index = list.index_of x
84           remove_at index
85   
86       def remove (i : u32) (x : T) =
87           list.remove i x
88   
89       def clear =
90           if is_not_empty then
91               for i = list.size - 1 downto 0 do
92                   remove_at i
93   
94       def as_readonly = self as List<T>
95