I've been investigating ".NET" and the C# language, and can't help compare things against Perl as well as the usual C++ and Java.

The author of a book I'm reading has a rather narrow world view, basically thinking that Microsoft products and Win32 are the entire history of computing. He makes sweeping statements, but obviously doesn't know other languages like Self, CLOS, Smalltalk, or Perl.

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.

Update: add example pseudocode

In pseudo-C++ (I don't want to butcher C# since I don't know it yet), it would be something like this:

class C { int x= 5; int y= 9; public: C() {}; // don't need to state x,y values. C (int xx) : x(xx) {} // y=9 without me saying it again. };
That is, for all constructors, not initializing a value will imply the global initialier's value, without specifying the same value in each constructor.

But, I seem to remember such a thing in one of the Perl object systems. Does anyone know which one that might be?

Second, what ever happened to ActiveState's Perl.NET? I heard lots of hype once upon a time, then reports that it didn't work but they are providing a seemless way to integrate Perl with .NET (which is all I care about the idea anyway). Has anybody used it and reported on it in detail yet?

--John

Replies are listed 'Best First'.
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

      You're thinking of default parameter values in C++. That's only good for the single function (e.g. constructor) in which it appears.

      I'm talking about not having to specify the (same) initial value for instance data among several constructors.

        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?

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.

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.

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.

      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.

        "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.
Why dont games use OOP?
by princepawn (Parson) on Dec 15, 2001 at 00:49 UTC
    Gaming systems often have complex object systems in practice (landscapes, monsters, players, projectiles, missions, etc), but for some reason most games that I know about are written in C.

    Does anyone know how thye manage complex object management without an object-oriented language?

      You may enjoy this article. Here's the introduction:

      Tim Sweeney co-founded Epic Games in 1991, providing the coding expertise that helped push the company forward over the next decade to become a leader in the computer 3D engine marketplace. The original Unreal was a major success in the states and especially abroad, thanks to an amazing array of engine features that attracted gamers and engine licensees alike. The sequel, Unreal Tournament, was all of that plus a year's worth of mutliplayer and network code painstakingly developed with extensive Internet playtesting. At the center of it all was Tim Sweeney, cranking away at the code. Tim's essay, "A Critical Look at Programming Languages," takes a hard look at the tools developers are using nowadays, and their shortcomings in the field of high-tech intensive 3D entertainment.

      --
      Mark Dominus
      Perl Paraphernalia

      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