Default constructor

Community Forums/Monkey Talk/Default constructor

Noobody(Posted 2011) [#1]
I came upon a slight annoyance in Monkey today. Like in most other OO languages, Monkey gives classes without user-defined constructors a default constructor that takes no parameters and only does basic initialization. Unlike other languages though, this default constructor still exists even when other constructors are defined. This means that a user of a class could accidentally or willingly bypass the constructors of a class and leave the new object in an uninitialized state by invoking New without supplying parameters.

It gets really annoying though when optional parameters come into play. Picture the following code:


One would expect Monkey to call the defined constructor with an argument of "" in the second New statement, but what it actually does is completely ignore the given constructor.

I lost an hour of debugging today because of this - a class of mine crashed constantly because it didn't get initialized (it had a constructor with default parameters as well). After I found out what actually happened, I tried to override the default constructor with a Method New() that calls the other constructor. But apparently there's no way to call another constructor; I tried New(Foo, Bar), Self.New(Foo, Bar) and Self(Foo, Bar), none of which seemed to work. The last expression even crashed the compiler with a "Null object access" :)

Would be neat if that could get fixed.


therevills(Posted 2011) [#2]
I wouldnt expect this at all.

In Java, if I had the following:

MyObject o = new MyObject();


I would need the following constructor:
    public MyObject()
    {
      setStr("HELLO");
    }   


I could also have another constructor defined like this:
    public MyObject(String a )
    {
      setStr(a);
    }


But to call the second constructor I would HAVE to call it like this:
MyObject o = new MyObject("TEST");



Noobody(Posted 2011) [#3]
If you had a class in Java without a constructor, the following would be valid:
MyObject o = new MyObject();

But if you do define a constructor (which takes parameters), the same call will give you a compile error.

In Monkey, the call would be valid in both cases, essentially bypassing all constructors that have parameters.

This is how OO compilers work normally: If you don't define a constructor, the compiler will define one for you - the default constructor. If you do define one, the default constructor won't be generated. Or more general: If you don't define a constructor, you can still instantiate that class. But if you define a constructor, you can't instantiate that class without calling the constructor correctly.


D4NM4N(Posted 2011) [#4]
If i remember in java (and C#) a default constructor is only valid if you do not define any others. If you do define one with params then you must add a default one manually otherwise it will throw.
Not tried it, but if mokey deviates from this then i would think it odd.

Last edited 2011


Warner(Posted 2011) [#5]
As in regular BMax, you can use a two-step creation method:



D4NM4N(Posted 2011) [#6]
If it allows that then i agree that is odd. Although that "create" method looks more like an initializer than a constructor.
Or is create special in some way? (its been ages since i used max)

in C#/Java/C++ it would be (similar) to:
Class Foo
{
       int _something
       public Foo()
       {
               consructor 1
        }

       public Foo(int something)
       {
               consructor 2
               _something=something
        }

        public static Foo Create()
       {
              an initializer
              return new Foo()
        }
        public static Foo Create(int something)
       {
              another initializer
              foo= new Foo()
              foo._something = something;
        }

}

however if i omitted the default Foo() then the default constructor would no longer work because i have another custom one.

Last edited 2011


therevills(Posted 2011) [#7]
But if you do define a constructor (which takes parameters), the same call will give you a compile error.


True.


In Monkey, the call would be valid in both cases, essentially bypassing all constructors that have parameters.


Ahhh got ya.

But I wouldnt have expected calling MyObject o = new MyObject(); would call my constructor with parameters since I hadnt said I wanted to use the parameters with the call.


taumel(Posted 2011) [#8]
This looks fine to me, i guess method overloading is supported by monkey so you would have needed to define a method for passing no parameter, otherwise this method is missing and you're running into an error.

Hmm you can't setup defaults for passed parameters this way.

It would be nice if the docs would be more detailed in this respect. Maybe another OOP tutorial like there once was for BlitzMax could help as well.

Last edited 2011


Warner(Posted 2011) [#9]
If it allows that then i agree that is odd. Although that "create" method looks more like an initializer than a constructor.
Or is create special in some way? (its been ages since i used max)

Well, Create does highlight in the BMax editor. However, it seems to be a regular initializer indeed. I use it on all my Types, because "New" doesn't allow parameters.