Ànnoying constructor problem....

Monkey Forums/Monkey Programming/Ànnoying constructor problem....

Vinians(Posted 2011) [#1]
How are you?
Well I am deriving several classes from my engine and I am having a very annoying problem with the builders 'new'. The problem is that if I need a builder I have to repeat in the derived class, as usual I would only need to redefine who I wanted to change something. There would fix this?


FlameDuck(Posted 2011) [#2]
Do you have an example? I'm not sure what you're trying to do.


Vinians(Posted 2011) [#3]
See:
Class Father extends Object
   Field x%
   Field y%
   Field z%
   Method Init()
       x = 0
       y = 0
       z = 0
   end
   Method New(x%, y%, z%)
      Init() 'this because monkey don let me call another constructor here
      self.x = x
      self.y = y
      self.z = z 
   End
   Method New()
      Init()
   End
End
Class Child Extends Father
   Field tag% 'anything 
End
'Using class
Function Main()
   Local ob:Child
   ob = New Child(1, 2, 3) 'Its doesnt work!! The child 
                           'class does NOT remember Father class
End

To work, I need to recreate the constructor on the Child class...
That is it!


Samah(Posted 2011) [#4]
Intended functionality, nothing to see here. Also you don't need to extend Object, it's implicit.


Foppy(Posted 2011) [#5]
You have to add a constructor to Child, but in that constructor you can call the constructor of the Father, using Super, so you don't have to copy it line for line. This will at least save you some typing.
Class Child Extends Father
   Field tag% 'anything
   
   Method New(x%, y%, z%)
      Super.New(x,y,z)
   End
End



AndyGFX(Posted 2011) [#6]
Only few changes:

Class Father
   
   Field x%
   Field y%
   Field z%

     Method New(x%=0, y%=0, z%=0)
      Self.x = x
      Self.y = y
      Self.z = z 
    End

  End

Class Child Extends Father
   Field tag% 'anything 
   Method New(x%=0, y%=0, z%=0)
      Super.New(x, y, z)
   End
End

'Using class
Function Main()
   Local ob:Child
   Local ob2:Child
   ob = New Child(1, 2, 3)
   ' or
   ob = New Child()
End



Vinians(Posted 2011) [#7]

You have to add a constructor to Child, but in that constructor you can call the constructor of the Father, using Super, so you don't have to copy it line for line. This will at least save you some typing.


That's exactly what I do now, the question is whether this should not be inherited from parent class, like Java.


Samah(Posted 2011) [#8]
Vinians: That's exactly what I do now, the question is whether this should not be inherited from parent class, like Java.

Constructors are not methods, therefore they do not get inherited. This is intended functionality with most OO languages, and it would very dangerous to allow external code to instantiate a class using its parent's constructors.

Method New(x%=0, y%=0, z%=0)

I wouldn't do this, because Monkey tends to do strange things with constructors that have all optional arguments. I suspect it's something to do with the default constructor that Monkey always generates (PLEASE get rid of this, Mark!)


FlameDuck(Posted 2011) [#9]
That's exactly what I do now, the question is whether this should not be inherited from parent class, like Java.
Constructors are not inherited in Java. In Java you still have to do this:

Java does give you a default constructor if you chose not to write your own, but so does Monkey (in fact monkey provides a default constructor, regardless of how many additional constructors you provide).


Samah(Posted 2011) [#10]
@FlameDuck: ...(in fact monkey provides a default constructor, regardless of how many additional constructors you provide).

And this is what I've been pushing for Mark to change and received no official response.



FlameDuck(Posted 2011) [#11]
And this is what I've been pushing for Mark to change and received no official response.
Yeah. It's a bit rubbish. What's worse is that it doesn't seem like you can say "Okay Monkey, here is your default constructor, but please make it private so that it can only be invoked by the object itself and not by random bits of code". Like what you would do with (say) a Singleton, which would be an acceptable workaround IMHO.


Samah(Posted 2011) [#12]
What I've been doing is adding this constructor (I know, I know, it's awful):
Method New()
  Error("Don't use default constructor.")
End