Is new-ing objects obsolete?

c#, java, language-design

Solution

SomeClassType x = SomeClassType();

in this case `SomeClassType()` might be a method located somewhere else, how would the compiler know whether to call this method or create a new class.

SomeClassType x;

This is not very useful, most people declare their variables like this and sometimes populate them later when they need to. So it wouldn't be useful to create an instance in memory each time you declare a variable.

Problem

Ok, so here's the question... is the new keyword obsolete? Consider in C# (and java, I believe) there are strict rules for types. Classes are reference types and can only be created on the heap. POD types are created on the stack; if you want to allocate them on the heap you have to box them in an object type. In C#, `structs` are the exception, they can be created on the stack or heap. Given these rules, does it make sense that we still have to use the new keyword? Wouldn't it make sense for the language to use the proper allocation strategy based on the type? For example, we currently have to write: ``` SomeClassType x = new SomeClassType(); ``` instead of ``` SomeClassType x = SomeClassType(); ``` or even just ``` SomeClassType x; ``` The compiler would, based on that the type being created is a reference type, go ahead and allocate the memory for x on the heap. This applies to other languages like ruby, php, et al. C/C++ allow the programmer more control over where objects are created, so it has good reason to require the new keyword. Is `new` just a holdover from the 60's and our C based heritage?

Original source