I do not know much about OO-languages, but from what I have seen, it seems most class-based OO-languages uses a keyword new
(or something equivalent) to create an object. Prototype-based OO-languages like JavaScript even fake it.
From a viewpoint of keeping the syntax simple, why not leave out the new
keyword and use only the constructor (typically of the same name as the class)?
Is there any semantic consideration involved in prefixing the constructor with a new
keyword?
I have noticed that in Scala, if you define a case
-class, you can simply use the constructor without a preceding new
to create an object, while for other classes, you have to use new
. I do not know the reason. I mention it simply because it may be related.
8
As far as I know, it’s simply a question of C++ having this syntax (and it needed some syntax because the language was not strong enough to support implementing new T();
at that time as a library) and then inherited. From memory, Java inherited it from C++, JavaScript picked it up from Java, and so did C#. Since then, it seems to have become pretty standard.
Fundamentally, it’s no different to using curly braces to denote scope- it was inherited from a common ancestor.