I heard about that Nim strings will become value types.
With latest Nim devel even nimble breaks, so it was clear that current gintro would not survive :-)
nimblepkg/packageinfo.nim(110, 25) Error: type mismatch: got <JsonNode, string, nil>
but expected one of:
proc optionalField(obj: JsonNode; name: string; default = ""): string
first type mismatch at position: 3
required type: string
but expression 'nil' is of type: nil
Indeed I had expected that there is no automatic conversion from empty Nim strings to NULL passed to C libs:
proc clib(s: cstring) =
echo s.isNil
echo s.len
echo cast[int8](s[0])
echo cast[int](s)
proc main =
clib("")
main()
At least GTK generally likes to get NULL pointers, and not cstrings with first char being value 0x0.
So I think I may apply a proc converting Nim "" to nil when C lib is called. For the rare case when user really wants to pass an empty cstring to C lib he may pass "x0" Nim string, maybe after calling setLen(1).
Yes indeed, as always mapping Nim "" to cstring nil may be very wrong.
As there seems to be no pragma or something similar to force "" to nil mapping, I am doing it this way now: For all string proc parameters which are attributed with "can be NULL" by gobject introspection data base, the default value for high level API is "". And only when this attribute is set, then "" is mapped to NULL for lib call. This seems to work -- for the rare cases when user wants to really pass an empty string for parameters that allow NULL, he has to pass "\x0".