www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - auto init & what the code means

Hello,

I have a problem with D's auto-init feature. When reading in someone else's=
 code
	int i;
there is no way, I guess, to know whether this means "i is initialised to 0=
" or "i is left undefined". For this reason, in the former case I do explic=
itely initialise. Thus,
	int i =3D 0;	// means initialised
	int i;		// means undefined _for me_
But, as _for me_ emphasizes, another reader still cannot guess the latter c=
ase.

I would enjoy having an idiom to express this clearly. Without it, a possib=
ility may be to use a conventional comment like
	int i;	// undef
in public code (esp Phobos) and spread the word about it. (Then, variable d=
eclarations without init or comment are known to be by people who do not us=
e this convention, and we know we need to search farther in code to interpr=
et them.)

Note that this problem does not apply to floats or pointers/refs, for which=
 .init is invalid for operating anyway. But there is a weird ambiguity abou=
t null/uninitialised arrays & strings which behave like empty in most cases=
. I would enjoy this to be clarified as well. For instance:
	int[] ints1 =3D [];
	ints1 ~=3D 1;		// ok
	int[] ints2;
	ints2 ~=3D 1;		// error
So that arrays behave like pointed/ref'ed thingies. (But I'm unsure about t=
he best way.) (*)

Denis

(*) This is also related to the boolean / null-compare value os arrays and =
strings:
void main()
{
    writeln("array -- undef");
    int[] ints1;
    writeln('(',ints1,')');
    if (ints1) writeln("ints1");
    if (ints1 !=3D null) writeln("ints1");
    if (ints1 !is null) writeln("ints1");
    ints1 ~=3D 1;
    writeln('(',ints1,')');
   =20
    writeln("array -- def");
    int[] ints2 =3D [];
    writeln('(',ints2,')');
    if (ints2) writeln("ints2");
    if (ints2 !=3D null) writeln("ints2");
    if (ints2 !is null) writeln("ints2");
    ints2 ~=3D 1;
    writeln('(',ints2,')');
   =20
    writeln("string -- undef");
    string str1;
    writeln('(',str1,')');
    if (str1) writeln("str1");
    if (str1 !=3D null) writeln("str1");
    if (str1 !is null) writeln("str1");
    str1 ~=3D '1';
    writeln('(',str1,')');
   =20
    writeln("string -- def");
    string str2 =3D "";
    writeln('(',str2,')');
    if (str2) writeln("str2");
    if (str2 !=3D null) writeln("str2");
    if (str2 !is null) writeln("str2");
    str2 ~=3D '1';
    writeln('(',str2,')');
} =20
=3D=3D>
array -- undef
()
([1])
array -- def
()
([1])
string -- undef
()
(1)
string -- def
()
str2
str2
(1)
-- -- -- -- -- -- --
vit esse estrany =E2=98=A3

spir.wikidot.com
Dec 26 2010