Feature Request List /
Auto Newing Of Classes
Auto New-ing of Classes at Declaration Time
All variables are pre-initialized in D to something useful. This is done to prevent the common bug of trying to use uninitialized variables. However, classes instances are initialized to null, which is mostly unhelpful.Based on the premise that the most frequent coding practice when declaring new instances of a class is to code ...
Foo AnInstance = new Foo;I propose that the syntax ...
Foo A;is a shorthand for ...
Foo A = new Foo;And that when the coder explictly does not want the class instance to be constructed they would code ...
Foo A = null;All current forms of constructor invocation syntax would remain untouched.
Discussion
I disagree - it would change the behaviour of existing code. -- StewartGordon
- (DerekParnell) Yes it would. But at what cost? We are not at v1.0 yet. Would be long term benefits out weigh that cost?
- (DerekParnell) Then there would be no change in behaviour. In other words, if the class did not have a parameterless constructor, then "Foo A;" would be exactly equivalent to "Foo A = null;"
- Seems a confusing inconsistency to me. -- StewartGordon
- I'm sorry. I'll use the term "default constructor" to mean "this(){ . . .}" for the purposes of this discussion. What I'm saying is that the rule would be - The idiom "[CLASSNAME] [ID];" would call the default constructor of class [CLASSNAME] if one has been defined otherwise it will invoke the default 'default constructor', which is to assign null to the [ID]. Seems so simple to me. I don't understand what it is that I'm missing. -- DerekParnell
- Simply the strangeness that the presence or absence of a default constructor alters the meaning of a declaration, and that an apparently uninitialised declaration should have a side effect under certain conditions. -- StewartGordon
- (DerekParnell) Then the coder would write "Foo A = new Foo(compile-time-constant);" just has they do now.
- No they don't. A constructor call doesn't evaluate to a compile-time constant, regardless of its arguments. -- StewartGordon
- I'm sorry, but I don't actually understand your point. Can you give an example of what you mean using current D syntax? -- DerekParnell
|
- or more simply
|
- Not valid code, as member initialisers have to be compile-time constants. -- StewartGordon
- (DerekParnell) Or in the case where the construction parameters were not known until run time, currently we code ...
|
|
- In what context requiring initialisers to be compile-time constants is this valid code? -- StewartGordon
- ?? It's not. I said "construction parameters were not known until run time" and thus they are not compile-time constants. Sorry I didn't make this clearer. -- DerekParnell
|
Not all variables are initialized to something useful, in fact, it's just the opposite (N|aN for floats, null for classes/arrays). The only exception are integral types, because they don't have a non-useful value in the same sense, so they're zeroed. The reason everything is initialized is not to provide a useful default value, but to prevent random behavior when using uninitialized variables. I think it would be best to leave things as-is, and perhaps introduce a shortcut like
new Foo a(x,y), b(z);
Reads better, if you ask me, and the construction is still explicit, like it should be. Or perhaps more simply, if the var name is followed by (params), it gets constructed; the problem there is that it looks exactly like a stack instance in C++, which may be problematic, because it isn't. -- XsZero?
Whatever! My whole point, and the one that people are not seeming to understand from my poor explanation, is that common idioms should be easy to write and read. That is the whole point of what I'm proposing. It has nothing to do with classes, initialization vs assignment, aggregates, C++, Java, etc, etc, etc,... Just that we should make reading and write code easier than it currently is. -- DerekParnell
IF D was to be consistent, we should be having to write
|
They all behave the same - "int a" allocates an integer on the stack and sets it to the default value, "int[] b" allocates a reference to an array on the stack and sets it to the default value (null), "myStruct c" allocates a struct on the stack and sets it to the default value, "myClass d" allocates a reference to an object on the stack and sets it to the default value (again null). What you want is actually different behavior for object references, not equal behavior. I don't have anything against a shorter syntax for "T t=new T", but it definitely should not be just "T t". -- XsZero?
- However, the runtime behaviour of a null array and a null object is very different. The null array is actually (in effect) an "object" with .length 0 and .ptr null, but it does not segfault when you try to access it like that (even if set to null) A NullObject would be the equivalent of a null array, so they are very different... -- AndersFBjörklund