>

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

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

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

${T}{\u2254}{table}{}\left(\left[{1}{=}{{x}}^{{2}}{\,}{{z}}^{{2}}{=}{2}\right]\right)$
 (2) 
>

$T\left[{z}^{2}\right]$

Do some other things, then trigger a collection
>

$1\:$$2\:$$3\:$$\mathrm{int}\left(\frac{1}{{\left({x}^{4}1\right)}^{2}}\,x\right)\:$

>

$\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.3em}{0.0ex}}\mathrm{assigned}\left(T\left[i\right]\right)\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\mathrm{val}\u2254T\left[i\right]\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{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.3em}{0.0ex}}\mathrm{assigned}\left(T\left[i\right]\,'\mathrm{val}'\right)\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\left(\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{if}\:$

Pattern#2: compare against evaln
>

$\mathrm{val}\u2254T\left[i\right]\:$$\mathbf{if}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathrm{val}\ne \mathrm{evaln}\left(T\left[i\right]\right)\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\left(\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{if}\:$

Pattern#3: use a sparse table
>

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

>

$\mathrm{val}\u2254T\left[i\right]\:$

>

$\mathbf{if}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathrm{val}\ne \mathrm{\_NOTFOUND}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\left(\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{if}\:$

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

$\mathrm{val}\u2254T\left[i\right]\:$

>

$\mathbf{if}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathrm{val}::\left(\neg \mathrm{indexed}\right)\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{then}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{2.0em}{0.0ex}}\left(\right)\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{if}\:$
