digitalmars.D - Fixed-size arrays and 'null'
- Max Samukha (22/22) Nov 12 2009 Please consider the following:
- bearophile (8/24) Nov 12 2009 In the Delight language this is disallowed:
- Max Samukha (6/30) Nov 12 2009 I'm not sure disallowing 'null' for T[] return values is a good idea.
- bearophile (4/5) Nov 12 2009 null is for a pointer/reference. A dynamic array in D is a struct. [] lo...
- Max Samukha (9/15) Nov 12 2009 Yes, but dynamic arrays in D are reference types (almost) and array
- Max Samukha (4/26) Nov 13 2009 Since nobody else has anything to say about this, I assume it is a
Please consider the following: import std.stdio; void foo(int[3] a) {} void main() { int[3] a = null; // 1 a = null; // 2 foo(null); // 3 if (a is null){} // 4 if (a == null){} // 5 } 1 - 2. These compile and issue runtime error. null is implicitly cast to int[] and the program tries to assign it at runtime. Even if it is not a bug, the error can be detected at compile-time. 3. Fails, but if 1-2 are ok, I can't see why. 4. Compiles but makes little sense. 5. This may be ok, if 1-2 are ok. I think all of the above should fail at compile time. Just disallow null initialization/assignment/comparison for fixed-size arrays. The new behavior has broken some of my code relying on a type's nullability and I would like to know if the semantics are officially approved (provided 3 and 4 are fixed)?
Nov 12 2009
Max Samukha:import std.stdio; void foo(int[3] a) {} void main() { int[3] a = null; // 1 a = null; // 2 foo(null); // 3 if (a is null){} // 4 if (a == null){} // 5 } 1 - 2. These compile and issue runtime error. null is implicitly cast to int[] and the program tries to assign it at runtime. Even if it is not a bug, the error can be detected at compile-time.In the Delight language this is disallowed: int[] foo() { return null; } You must use something like: int[] foo() { return []; } That I think is better/more clear. Bye, bearophile
Nov 12 2009
On Thu, 12 Nov 2009 07:42:57 -0500, bearophile <bearophileHUGS lycos.com> wrote:Max Samukha:I'm not sure disallowing 'null' for T[] return values is a good idea. What's the advantage? Anyway, it's a different matter. Mingling *fixed-size* arrays with null does make little sense.import std.stdio; void foo(int[3] a) {} void main() { int[3] a = null; // 1 a = null; // 2 foo(null); // 3 if (a is null){} // 4 if (a == null){} // 5 } 1 - 2. These compile and issue runtime error. null is implicitly cast to int[] and the program tries to assign it at runtime. Even if it is not a bug, the error can be detected at compile-time.In the Delight language this is disallowed: int[] foo() { return null; } You must use something like: int[] foo() { return []; } That I think is better/more clear. Bye, bearophile
Nov 12 2009
Max Samukha:What's the advantage?null is for a pointer/reference. A dynamic array in D is a struct. [] looks much better to denote an empty array-struct. Bye, bearophile
Nov 12 2009
On Thu, 12 Nov 2009 13:53:13 -0500, bearophile <bearophileHUGS lycos.com> wrote:Max Samukha:Yes, but dynamic arrays in D are reference types (almost) and array references (doesn't matter whether they are bare pointers or fat references) can be tested for nullness and set to null. Like it or not, there is a distinction between a null array and an empty array, so I would prefer to use 'null' wherever I want to say 'null array reference', meaning the struct's ptr member is definitely null.What's the advantage?null is for a pointer/reference. A dynamic array in D is a struct.[] looks much better to denote an empty array-struct. Bye, bearophile
Nov 12 2009
On Thu, 12 Nov 2009 12:18:26 +0200, Max Samukha <spambox d-coding.com> wrote:Please consider the following: import std.stdio; void foo(int[3] a) {} void main() { int[3] a = null; // 1 a = null; // 2 foo(null); // 3 if (a is null){} // 4 if (a == null){} // 5 } 1 - 2. These compile and issue runtime error. null is implicitly cast to int[] and the program tries to assign it at runtime. Even if it is not a bug, the error can be detected at compile-time. 3. Fails, but if 1-2 are ok, I can't see why. 4. Compiles but makes little sense. 5. This may be ok, if 1-2 are ok. I think all of the above should fail at compile time. Just disallow null initialization/assignment/comparison for fixed-size arrays. The new behavior has broken some of my code relying on a type's nullability and I would like to know if the semantics are officially approved (provided 3 and 4 are fixed)?Since nobody else has anything to say about this, I assume it is a bug.
Nov 13 2009