[Issue 3889] New: Forbid null as representation of empty dynamic array
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Mar 7 07:08:14 PST 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3889
Summary: Forbid null as representation of empty dynamic array
Product: D
Version: 2.040
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-03-07 07:08:13 PST ---
I prefer a language that uses literals that are as much as possible clear,
readable and unambiguous. In D "null" can represent empty pointers, empty class
references, and empty dynamic arrays:
int[] foo(int[] a) {
return null;
}
int[] foo2(int[] a) {
return [];
}
void main() {
foo(cast(int[])null);
foo(null);
foo([]);
foo2(cast(int[])null);
foo2(null);
foo2([]);
}
But "null" is not a clear way to represent an empty array, that is a struct of
two items (this reminds me of the untidy usage of 0 to represent a null pointer
in C). So I propose to remove the usage of "null" to represent an empty dynamic
array (the Delight D-derived language already allows only [] for such purpose).
Note: [] is not fully unambiguous, it doesn't specify a type. Specifying a type
can be useful, because you can use it to give as argument to a template
function an array of the correct type:
import std.stdio: writeln;
void foo(T)(T[] a) {
writeln(typeid(T));
}
void main() {
// OK, int[]
foo(cast(int[])null);
// OK, int[]
foo(cast(int[])[]);
// Partially OK, inside foo 'a' is of type void[]
foo([]);
// Bad, Error: template test.foo(T) does not match any function template
declaration
foo(null);
}
In such situations cast(int[])[] is enough. I think this idiom isn't common in
D code.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list