Null references redux

Steven Schveighoffer schveiguy at yahoo.com
Tue Sep 29 09:05:13 PDT 2009


On Mon, 28 Sep 2009 21:43:20 -0400, Jesse Phillips  
<jessekphillips at gmail.com> wrote:

> On Mon, 28 Sep 2009 16:01:10 -0400, Steven Schveighoffer wrote:
>
>> On Mon, 28 Sep 2009 15:35:07 -0400, Jesse Phillips
>> <jesse.k.phillips+d at gmail.com> wrote:
>>
>>> language_fan Wrote:
>>>
>>>> Have you ever used functional languages? When you develop in Haskell
>>>> or SML, how often you feel there is a good change something will be
>>>> initialized to the wrong value? Can you show some statistics that show
>>>> how unsafe this practice is?
>>>
>>> So isn't that the question? Does/can "default" (by human or machine)
>>> initialization create an incorrect state? If it does, do we continue to
>>> work as if nothing was wrong or crash? I don't know how often the
>>> initialization would be incorrect, but I don't think Walter is
>>> concerned with it's frequency, but that it is possible.
>>
>> It creates an invalid, non-compiling program.
>
> No it doesn't, I'm not referring to null as the invalid state.
>
> float a;
>
> In this program it is invalid for 'a' to equal zero. If the compiler
> complains it is not initialized the programmer could fulfill the
> requirements.

I am not arguing for floats (or any value types) to be required to be  
initialized.

>
> float a = 0;
>
> Hopefully the programmer knows that it shouldn't be 0, but a correction
> like this is still possible, the compiler won't complain and the program
> won't crash. Depending on what 'a' is controlling this could be very bad.
>
> I'm really not arguing either way, I'm trying to make it clear since no
> one seems to be getting Walters positions.

I get his arguments, but I think they are based on an non-analagous  
situation.  I think his arguments are based on his experience with  
compilers or corporate rules requiring what you were saying --  
initializing all variables.  We don't want that, we just want the  
developer to clarify "this variable is initialized" or "this variable is  
ok to be uninitialized".

> BTW, what is it with people writing
>
> SomeObject foo;
>
> If they believe the compiler should enforce explicit initialization? If
> you think an object should always be initialized at declaration don't
> write a statement that only declares and don't set a reference to null.

It's more complicated than that.  For example, you *have* to write this  
for objects that are a part of aggregates:

class SomeOtherObject
{
   SomeObject foo; // can't initialize here, because you need to use the  
heap, and compiler only allows CTFE initialization.

   this()
   {
      foo = new SomeObject(); // here is where the initialization sits.
   }
}

This is ok, but what if the initialization is buried, or you add another  
variable to a large class and forgot to add the initializer to the  
constructor?

And there *are* cases where you *don't* want to initialize, that should  
also be possible:

SomeObject? foo;

If this wasn't part of the proposal, I'd agree with Walter 100%, but it  
gives the lazy programmer an easy way to default to the current behavior  
(easier than building some dummy object), so given the lazy nature of said  
programmer, they are more likely to do this than assign a dummy value.

-Steve



More information about the Digitalmars-d mailing list