Using the wildcard pattern with use works within a sequence expression, but not otherwise. Is there a reason for this?
let mkDisposable() =
{ new IDisposable with
member __.Dispose() = () }
let mkSeq() =
seq {
use _ = mkDisposable() //OK
()
}
let f() =
use _ = mkDisposable() //ERROR: 'use' bindings must be of the form 'use <var> = <expr>'
()
I believe that this is a natural (but unexpected) consequence of the desugaring of computation expressions (a sequence expression in this case, but this behavior applies to all computation expressions). As the spec indicates,
is translated to
Because this is a shallow syntactic translation, you can use any pattern that could be used when writing a function, including just
_. However, for normalusebindings, the left hand side of the binding must be an identifier, not a pattern (see section 6.6.3 of the spec).