New() = constructor?

BlitzMax Forums/BlitzMax Programming/New() = constructor?

Warren(Posted 2004) [#1]
I've started to take to a practice of treating "Method New()" overrides as a constructor function for my types.

Is this a bad idea? Is there another way to achieve a constructor like interface or is THIS the way to do it?


soja(Posted 2004) [#2]
That's the way to do it. Well, it's one way, but it's the more "object-oriented" way. You'll find examples of another way in the samples, though, where they use something like "Function Create", put the constructor code in there, and call it from external code.

I prefer writing a New method, since it extends the base type and is called automatically.


Perturbatio(Posted 2004) [#3]
The problem with using new is that you cannot pass arguments to it, so if you want to create a new type with values that are not know at design time then you need to make your own Create method and pass them via that or manually initialize them.

Personally, I like the method Create approach.


FlameDuck(Posted 2004) [#4]
Is this a bad idea?
Not really, no.

Is there another way to achieve a constructor like interface or is THIS the way to do it?
For a constructor like interface, THIS is the way to do it.

Well, it's one way, but it's the more "object-oriented" way.
No it isn't. The Simple Factory, or Abstract Factory design patterns are the 'more object-oriented' way to do it.

The problem with using new is that you cannot pass arguments to it
Yeah, you can.

The 'problem' with using New, is that it never fails (even if it actually fails), but rather returns a null-pointer instead.


Perturbatio(Posted 2004) [#5]
The problem with using new is that you cannot pass arguments to it
Yeah, you can.


The docs need updated then:
User defined types can optionally declare two special methods named New and Delete. Both methods must take no arguments, and any returned value is ignored



Warren(Posted 2004) [#6]
I'm not looking to do anything complicated, I just want to have a place to hook into for allocating internal objects, setting default values other than zero/null, etc.

Specialized construction requiring arguments passed in and such I would fully expect to have to implement through a Create function or something similar.

P.S. Why does talk of "design patterns" always cause my 'urge to punch' to rise violently?


Warren(Posted 2004) [#7]
The 'problem' with using New, is that it never fails (even if it actually fails), but rather returns a null-pointer instead.

I don't really worry about that. If 'New' is failing, you have larger problems to worry about...


Perturbatio(Posted 2004) [#8]
Why does talk of "design patterns" always cause my 'urge to punch' to rise violently?


The same reason as Power Nap, Power Walk and all those other phrases do (for me at least)?