| 1 |
= MLtonWeak |
| 2 |
|
| 3 |
[source,sml] |
| 4 |
---- |
| 5 |
signature MLTON_WEAK = |
| 6 |
sig |
| 7 |
type 'a t |
| 8 |
|
| 9 |
val get: 'a t -> 'a option |
| 10 |
val new: 'a -> 'a t |
| 11 |
end |
| 12 |
---- |
| 13 |
|
| 14 |
A weak pointer is a pointer to an object that is nulled if the object |
| 15 |
becomes <<Reachability#,unreachable>> due to garbage collection. The |
| 16 |
weak pointer does not itself cause the object it points to be retained |
| 17 |
by the garbage collector -- only other strong pointers can do that. |
| 18 |
For objects that are not allocated in the heap, like integers, a weak |
| 19 |
pointer will always be nulled. So, if `w: int Weak.t`, then |
| 20 |
`Weak.get w = NONE`. |
| 21 |
|
| 22 |
* `type 'a t` |
| 23 |
+ |
| 24 |
the type of weak pointers to objects of type `'a` |
| 25 |
|
| 26 |
* `get w` |
| 27 |
+ |
| 28 |
returns `NONE` if the object pointed to by `w` no longer exists. |
| 29 |
Otherwise, returns `SOME` of the object pointed to by `w`. |
| 30 |
|
| 31 |
* `new x` |
| 32 |
+ |
| 33 |
returns a weak pointer to `x`. |