>

$\mathrm{alive}\u2254{z}^{2}$

${\mathrm{alive}}{\u2254}{{z}}^{{2}}$
 (1) 
>

$T\u2254{\mathrm{table}}_{\mathrm{weak\_key}}\left(\left\{1\={x}^{2}\,{z}^{2}\=2\right\}\right)$

${T}{\u2254}{table}{}\left(\left[{1}{=}{{x}}^{{2}}{\,}{{z}}^{{2}}{=}{2}\right]\right)$
 (2) 
Do some other things, then trigger a collection
>

$1\:$$2\:$$3\:$$\∫\frac{1}{{\left({x}^{4}1\right)}^{2}}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\ⅆx\:$

>

$\mathrm{gc}\left(\right)$

The T[1] entry has been removed. The T[z^2] entry's key is referenced in the variable alive, so it stays in the table.
>

$\left[\mathrm{entries}\left(T\,\'\mathrm{pairs}\'\right)\right]$

$\left[{{z}}^{{2}}{=}{2}\right]$
 (5) 
Because table indexing can return unevaluated, it is a common pattern in code to do something like this:
>

$\mathbf{if}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathrm{assigned}\left({T}_{i}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathrm{val}\u2254{T}_{i}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{end\; if}\:$

With weaktables, the entry at T[i] could disappear between the assigned() check and the fetch to assign to val, possibly leading to strange and hard to reproduce errors. There are several ways to safely work around this.
Pattern#1: check assigned and get the value all at once:
>

$\mathbf{if}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathrm{assigned}\left({T}_{i}\,\'\mathrm{val}\'\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{end\; if}\:$

Pattern#2: compare against evaln
>

$\mathrm{val}\u2254{T}_{i}\:$$\mathbf{if}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathrm{val}\ne \mathrm{evaln}\left({T}_{i}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{end\; if}\:$

Pattern#3: use a sparse table
>

$T\u2254{\mathrm{table}}_{\mathrm{weak\_key}}\left(\mathrm{sparse}\=\mathrm{\_NOTFOUND}\right)\:$

>

$\mathrm{val}\u2254{T}_{i}\:$

>

$\mathbf{if}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathrm{val}\ne \mathrm{\_NOTFOUND}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{end\; if}\:$

Pattern#4: check that the value is an expected type
>

$\mathrm{val}\u2254{T}_{i}\:$

>

$\mathbf{if}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathrm{val}::\left(\mathrm{Not}\left(\mathrm{indexed}\right)\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}\mathbf{end\; if}\:$
