Can we not use default arguments with "New" method

Monkey Forums/Monkey Programming/Can we not use default arguments with "New" method

matty(Posted 2011) [#1]
Strict

Function Main:Int()

Local myclass:MyClass

myclass = New MyClass()

Print myclass.x
Print myclass.y
Print myclass.z

Return 0

End Function 

Class MyClass

Field x:Int
Field y:Int
Field z:Int

Method New(lp_x:Int=1,lp_y:Int=2,lp_z:Int=3)

x = lp_x
y = lp_y
z = lp_z

End Method

End Class


The output I get is
0
0
0

EDIT : I realise this may be similar to the post in this thread (http://www.monkeycoder.co.nz/Community/posts.php?topic=1105) but wasn't sure.

It took me ages to work out why I was getting a blank white screen when it turned out my sprites scales were all being kept at a scale of 0 when I thought I was defaulting them to '1'.


Samah(Posted 2011) [#2]
In its ultimate wisdom, Monkey decides that all classes should have a default constructor, even if you don't want one.

It appears that since you aren't passing any arguments it's calling the default constructor instead. In the Diddy GUI I've actually put default constructors with an Error call in them to stop this kind of thing happening.

This is a bug. Unless there are no constructors defined at all, Monkey shouldn't create a default one for you. I'm hoping Mark will do something about it. :)


Skn3(Posted 2011) [#3]
Agree that there should be no default constructor. It does become quite awkward when deciding on object initialisation design.


ziggy(Posted 2011) [#4]
Agree too, but this would have some side effects on the inheritance design (I think).


Gerry Quinn(Posted 2011) [#5]
I think the problem is not the existence of a default constructor, but that Monkey is not clever enough to work out that your constructor with default initialisers should take its place.


Samah(Posted 2011) [#6]
Agree that there should be no default constructor. It does become quite awkward when deciding on object initialisation design.

A default constructor is fine unless you have a non-default one defined. If I have no constructor defined, I'd expect Monkey to make a default constructor with no body. If I've defined a constructor that takes arguments, Monkey should *not* generate a default.

I think the problem is not the existence of a default constructor, but that Monkey is not clever enough to work out that your constructor with default initialisers should take its place.

True, but if your constructor DOESN'T have default values, it should throw an error when you try to call the default no-arg constructor (if there isn't one).


Skn3(Posted 2011) [#7]
Yeah I think the suggestion of monkey giving priority to user defined constructor with defaults is a good solution eg if there is matching override but defaults are used then avoid unless other alternative is the default constructor.