&?Box is “a Box or nothing”; &Box is “definitely a Box”. Converting the first into the second with to &Box is a promise that it isn’t null, and Turf only accepts that promise where it can see it kept. Copying the value out with to Box reads through the reference, so it needs the same check.
struct Box { val: int }fn bad(b: &?Box): int { mut safe: &Box = b to &Box; // b might be null return safe.val;}Check it first, and convert inside the check:
fn good(b: &?Box): int { if b != null { mut safe: &Box = b to &Box; // proven non-null here return safe.val; } return 0;}Often you don’t need the conversion at all: inside the if b != null { ... }, b can be used directly, and passed, returned or stored as a &Box as it is.
The same error is reported when a &?Box is used as a &Box without the check (return b; from a function that returns &Box, or f(b) for an f that takes one).
A variable can be checked, and so can a field reached from one (h.item), until the next call or the next write to a field with that name:
struct Holder { item: &?Box }fn good(h: &Holder): int { if h.item != null { mut safe: &Box = h.item; return safe.val; } return 0;}A &?Box from a call or a list element is stored in a variable first, then checked and converted.
Run turf --explain E082 in a terminal to read this again.