Re: OOP,
by Masem (Monsignor) on Dec 14, 2001 at 23:00 UTC
|
Default initiailizer values are a standard in the C++ language. (I'd quote the C++ reference, but I don't have it handy). I remember using this to also have functions with 'optional' arguments that would be filled in by the default initializers.
Now, while Perl doesn't have this directly, it's very easy to do , particullary if you pass arguments by hash. Damian's OOP has examples like:
my %func_defaults = ( a=>3, b=>4, c=>5 );
sub func {
%my_args = ( %func_defaults, @_ );
...
}
Or of course, you have friendly operators like ||= (or //= that is in Perl6).
It's not as 'simple' as C++ (or C#, apparently)'s default arguments, but they're still there.
I know Java doesn't have this feature, nor C.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Are you familiar with "fallthrough" constructors? you only specify a given parameter at the appropriate "level" of constructor .
given our object with say, x and y and z:
object(xx,yy,zz) {
x=xx; y=yy; z=zz
}
object (xx,yy) {
object(xx,yy,defaultz)
}
object (xx) {
object(xx,defaulty)
}
object (){
object(defaultx)
}
close? | [reply] [d/l] |
|
|
Re: OOP,
by dragonchild (Archbishop) on Dec 14, 2001 at 23:11 UTC
|
I'm a little confused. Supplying a default initial value for a data member when it's declared ...
*thinks for a moment*
*springs up and shouts* Perl does do that!
Here's a standard constructor in Perl:
sub new {
my $class = shift;
my %args = @_;
my $self = bless {}, $class;
$self->{attrib1} = $args{attrib1} || 'Default 1';
$self->{attrib2} = $args{attrib1} || 'Default 2';
return $self;
}
My point here is that, since Perl doesn't have a mechanism to (natively) define a class in a series of declarative statements (like C++, for example), the "class declaration" is done during new().
Now, you can do other stuff. I have my own personal virtual base class that gives me the capability to "declare" a class at compile time, with default values. I could post it, if desired. *shrugs* It's not that hard!
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. | [reply] [d/l] |
Re: OOP,
by hding (Chaplain) on Dec 14, 2001 at 23:39 UTC
|
Anyway, one thing he points out is the ability to supply a default initial value for a data member when it is declared,
and not have to specify that value as an initializer in every contructor (though it has that effect). He says (implied
all) other OO languages can't do that.
Just for the record (and you quite possibly realize this
already), it is possible to do this in Common Lisp by specifying an
:initform argument in the slot definition for a class. In fact,
as the name suggests, you can do even better than specify a
value; you can specify any initialization form you want.
I can't think exactly how you'd do in in Smalltalk, though you can
of course simulate it a bit (as you can in many other languages)
by defining a hierarchy of constructors that call each other,
supplying defaults along the way.
| [reply] |
Re: OOP,
by perrin (Chancellor) on Dec 14, 2001 at 22:46 UTC
|
Is it really such a chore to put the initial default value for instance data in the constructor? You have to put it somewhere, and that seems like the logical place to me. | [reply] |
|
|
If you have a variety of different constructors, though, it
would seem to be possible that two of them might need to
specify the same default value for a variable (and it might
be impossible to rig things up so that one could get it by
invoking the other at some point), so something like this
would help keep things in sync if a change ever needed to be
made.
| [reply] |
|
|
"a variety of different constructors"? I may be a newcomer to OOP, but that's a recipe for disaster unless you factor out the commonalities at the package level and create private methods that do those parts for your various public constructors.
Another solution, if you absolutely insist on a myriad of monolithic constructor routines (again, these sound like a maintenance nightmare), is to create a package global that holds a default $self to be assigned when you declare my $self the first time in each constructor, and then over-ridden as the parameters are dealt with.
| [reply] |
|
|
|
|
Why dont games use OOP?
by princepawn (Parson) on Dec 15, 2001 at 00:49 UTC
|
| [reply] |
|
|
| [reply] |
|
|
Look at the code for Nethack. While there are several 'objects', there is very little inheritence, thus, they only need to keep a 'monster' array, a 'object' array, etc. In such a flat OOP system, C is very easy to use to mimic a true OOP language. The benefits of 'switch' become highly apparent here.
Mind you, you *can* rewrite Nethack to use true OOP. As an example, you could easily use Java Interface-equivalent classes to describe all the attrbiutes that monsters may have (say, "class Large", "class Intelligent", etc). Then you can create new monsters by simply creating a new class and 'inheriting' those attributes ("class Tiamat implements Large, Intelligent, Flying, FireBreathing, PitA { ... }" ).
There's even sort of a meta-OOP floating with Nethack; one can define which interface they want at make/build time; this allows the Nethack team to actually do a primative MVC architecture so that those running the game on more 'graphic' terminals can chose an interface that's more suited for that.
But just because you can doesn't mean it will work. I know of numerous attempts to OOP'ive the classic Rogue-like games with no luck.
Just remember, any OOP structure/intherience tree/class diagram can be flatted to a procedural structure. (Initial versions of C++ did just this; they preprocessed code to munge class names and methods to unique C names) In most cases, this will be messy and filled with lots of switch blocks, but it can do the same thing. However, the flatter the object structure starts, the simplier the underlying C code, and this typically can be optimized further to get speed gains.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important
| [reply] |