www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - null to delegate cast?

reply icee <iceelyne gmail.com> writes:
In http://www.digitalmars.com/d/expression.html, you said
"A null is implicitly converted to a Type delegate() by creating an
anonymous delegate that returns null."

But the truth seems not:

//char[] delegate () dg = null; //cause av
char[] delegate () dg = delegate char[]() {return null;};
try{dg();}catch(Exception e){writefln(e.msg);}

or, if i made any mistakes here?


null
The keyword null represents the null pointer value; technically it is of
type (void *). It can be implicitly cast to any pointer type, including
pointers to functions. The integer 0 cannot be cast to the null pointer.
Nulls are also used for empty arrays.

A null is implicitly converted to a Type delegate() by creating an anonymous
delegate that returns null. To get an actual null value for the delegate,
use one of the following:

const Type delegate() dgnull;   // default initializer for delegate is null
...
Type delegate() dg1;  // default initializer for delegate is null
Type delegate() dg2 = dgnull;
Type delegate() dg3 = (Type delegate()).init;
Type delegate() dg4 = cast(Type delegate()) null;

Type delegate() dg5 = null;  // initializes dg5 to
                             // delegate Type() { return null; }
Sep 10 2006
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
icee wrote:
 In http://www.digitalmars.com/d/expression.html, you said
 "A null is implicitly converted to a Type delegate() by creating an
 anonymous delegate that returns null."
 
 But the truth seems not:
 
 //char[] delegate () dg = null; //cause av
Do you mean: (a) that merely assigning null to a delegate variable causes an AV? (b) that calling dg afterwards causes an AV? (a) certainly shouldn't be happening. But if (b), of course it does. What else would it do? <snip>
 const Type delegate() dgnull;   // default initializer for delegate is null
 ....
 Type delegate() dg1;  // default initializer for delegate is null
 Type delegate() dg2 = dgnull;
 Type delegate() dg3 = (Type delegate()).init;
 Type delegate() dg4 = cast(Type delegate()) null;
 
 Type delegate() dg5 = null;  // initializes dg5 to
                              // delegate Type() { return null; }
Case dg5 is certainly weird. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Sep 11 2006
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Stewart Gordon wrote:
 icee wrote:
 In http://www.digitalmars.com/d/expression.html, you said
 "A null is implicitly converted to a Type delegate() by creating an
 anonymous delegate that returns null."

 But the truth seems not:

 //char[] delegate () dg = null; //cause av
Do you mean: (a) that merely assigning null to a delegate variable causes an AV? (b) that calling dg afterwards causes an AV? (a) certainly shouldn't be happening. But if (b), of course it does. What else would it do? <snip>
 const Type delegate() dgnull;   // default initializer for delegate is 
 null
 ....
 Type delegate() dg1;  // default initializer for delegate is null
 Type delegate() dg2 = dgnull;
 Type delegate() dg3 = (Type delegate()).init;
 Type delegate() dg4 = cast(Type delegate()) null;

 Type delegate() dg5 = null;  // initializes dg5 to
                              // delegate Type() { return null; }
Case dg5 is certainly weird.
That is a leftover from implicit conversions of expressions (null in this case) to delegates, it shouldn't be a problem any more.
Sep 11 2006
parent icee <iceelyne gmail.com> writes:
So either the doc or the dmd should be fixed.
Maybe the doc cos' implicit anonymous delegate creation for null is not very
useful.
Sep 11 2006