Can you have multiple constructors? I'm getting a "Redefinition of 'initialize'" error

Can you have multiple constructors? I'm getting a "Redefinition of 'initialize'" error when trying you use multiple initialization methods. For example, I'm trying to use the following:

function initialize() { }

And then following:

function initialize(themeNumber as Number) { }

Thanks.

  • He already has two types (null and Number). There's no magic rule that says he can't add another.

    (Don't really care what the name of the variable is.)

    Monkey C calling an integer "Number" was a bad decision.

    ------------------------------------------------------------

    (I also expected the testing-against-null option would have been obvious given the declaration of the argument. People are free to put some thought into what they read.)

  • When I see typed code I assume it's there for a reason, and the reason is not "for fun, but the typechecker is turned off"...
    So when the function was declared as Number and used with -l 3 then there's no need to type check *.

    *) If you want to cause problems to yourself you can trick it:
    var n = null;
    new Class(n) => fails
    new Class(n as Number) => compiles, but will get a null pointer exception inside if n is used without checking.

    Also for the same reason, assuming you're not hacking yourself, if you declare it as Number or Number? then you can IMHO assume it's either a Number or Null, no need to assume it can also be anything else. If you do, then better declare it accordingly (you can also use Any)

  • I tried it a couple ways and couldn't get it to work. Using the following:

    function initialize(themeNumber_ as Number or Null) {

    It compiles, but crashes with the message: Trying to call function '$.Theme.initialize' with the wrong number of arguments.

    With another way I thought maybe would work:

    function initialize(themeNumber_ as Number or null) {

    It doesn't compile with the following message: mismatched input 'or' expecting {')', ','}

    I know I can get  by without doing this, but I'm just trying to use best practices.

  • You need to call the initializer with an argument (even if it's null).

  • It has to be "Null" (not "null") in the definition.

    class ServerObj
    {
        var name as String;
        var url as String;

        function initialize(num as Number or Null)
        {
            name = "";
            url = "";
        }
    }
     
                var serverNew = new ServerObj(null) as ServerObj;