In C we may pass a pointer to a valid storage location to functions when we are interested in that out parameter, and NULL when not. Full story at
https://github.com/StefanSalewski/gintro/issues/102
demotomohiro explained recently that we can indeed fake that behaviour for Nim's var parameters:
type
Store = ref object
i: int
proc p(s: var Store = cast[ptr Store](nil)[]) =
echo addr(s) == nil
proc p2(s: var Store = cast[var Store](nil)) =
echo addr(s) == nil
proc main =
var x: Store
p(x)
p()
p2(x)
p2()
main()
With output
false
true
false
true
We should remember that when we write C lib bindings with optional out parameters.
The notation castptr Store[] seems to work only in proc headers, I thought before it was not allowed at all. But castvar Store seems to work also. Should we prefer one of them? In the proc the test addr(s) == nil seems to be simple.