Thread overview
null to delegate cast?
Sep 11, 2006
icee
Sep 11, 2006
Stewart Gordon
Sep 11, 2006
Ivan Senji
Sep 12, 2006
icee
September 11, 2006
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; }
September 11, 2006
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.
September 11, 2006
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.
September 12, 2006
So either the doc or the dmd should be fixed.
Maybe the doc cos' implicit anonymous delegate creation for null is not very
useful.