Page 1 of 1

Allow 0 to be used with pseudotypes

Posted: Thu May 10, 2012 11:01 am
by remi_meier
Some external libraries take strings as arguments where
one is allowed to pass a 0 (null pointer) instead of an actual
string. Most of the time this means that some default value
will be used instead.

Currently there are two solutions, one is not using pseudotypes
which requires manual conversion and the other using an
uninitialized string variable like so:

Code: Select all

s.s
Debug @s
function_call(s)
As pseudotypes are mostly used for interfacing with external
functions and since a null-pointer is actually a valid "empty
string" in PureBasic, I propose to allow passing a literal 0
for a pseudotype-argument. I do not think that this functionality
would also be useful for normal string type arguments. Hence
this request is only for pseudotypes.

Example:

Code: Select all

ImportC "externalLib"
  bla(u.p-ascii)
EndImport

bla("")
u.s = ""
bla(u) ; <- the same

bla(0) ; <- not the same!
s.s
bla(s) ; <- the same as bla(0)

Additional consequences:
Currently there is some inconsistent behaviour. If I compile
in UNICODE mode, then any conversion to p-unicode will
be ignored (OK). But if I switch to compiling in ASCII mode,
it seems that even for the uninitialized string a conversion
to unicode is done and PB actually allocates a new string.

For my proposal to work, all pseudotype-conversions must
not touch a null-pointer (uninitialized string) and just pass it
on to the C function as a null-pointer.


Otherwise:
If the PB team is not inclined to implement this request,
please remove the inconsistency and always do a conversion
even if it is a ASCII-ASCII conversion so we never get to
pass a null-pointer by accident.


Cheers
Remi