Optional default constructor

Monkey Forums/Monkey Programming/Optional default constructor

Samah(Posted 2011) [#1]
Time to open this can of worms again...

Can you please make default constructors work like in Java and many other languages? If there are no constructors defined, Monkey should generate a New(). If there are other constructors defined, Monkey should not generate a New() constructor.
Class Foo
End

Class Bar
  Method New(hello:String)
  End
End

Class Test
  Method New()
  End

  Method New(hello:String)
  End
End

New Foo ' this should work, New() is generated automatically
New Bar("test") ' this should work, New(:String) is defined by the dev
New Bar ' this should fail, New() is not defined by the dev, and another constructor exists
New Test ' this should work, New() is defined by the dev

If a New() constructor is generated when the developer doesn't want one, it allows objects to be instantiated in an invalid state. This is bad for framework developers.

This would also force developers to explicitly call a superconstructor if Super.New() did not exist.


Tibit(Posted 2011) [#2]
The way I do it when I do not want the user to be able to call new is to make the empty new private. Yet I agree, it looks like a small change so why not I guess?