C# interview
Benji Smith
dlanguage at benjismith.net
Tue Oct 7 07:02:49 PDT 2008
Adam D. Ruppe wrote:
> On Tue, Oct 07, 2008 at 02:21:23PM +0200, Don wrote:
>> My point is simply that:
>>
>> Object o;
>>
>> is almost always an error, and I think that making it illegal would
>> catch the #1 trivial bug in D code.
>
> I agree here. When I first started with D, I would write that almost
> every time I was working with an object, and it would segfault without
> fail.
>
> In C++, writing Object o; is quite common, and as a C++ user coming to D,
> it took me a while to get away from that habit. I'm sure other C++ users
> have had the same experience.
>
> This change definitely seems like a good idea. If it breaks existing code,
> it is easy enough to add the = null if you need it, so there is little cost
> and I think a decent gain.
On the flip side, as a Java programmer, I write "Object o;" all the
time, and it's generally NOT an error. For example:
class MyObject {
Object a;
Object b;
Object c;
public this(Object a, Object b, Object c) {
this.a = a;
this.b = b;
this.c = c;
}
}
I like to leave object references uninitialized in my class member
declarations, and then assign them in the constructor.
I'd be disappointed if those kinds of declarations were illegal, and I
was required to initialize them with "= null" every time.
--benji
More information about the Digitalmars-d
mailing list