in reply to Re: perl6 & OO
in thread perl6 & OO

Exactly. I'd like methods give compile errors if their arguments if they are the wrong type. Even using mechanisms like strict, etc, Perl is a little dangerous when it comes to OO syntax. When I'm writing a straightforward non-OO app, this is fine, and I can deal with it, but when I really want objects, I want Perl to understand them and enforce them, rather than allowing them to be scalars. I think this could be accomodated for in the oft-unused Perl message-signature feature, which right now only takes basic types.

Also, it's really odd what determines when a class has a certain method. For instance, if I have a "$program->run" and a "$dog->run" I don't want to be able to call $foo->run() and have it work with both dogs and programs. I want method overloading. I want cleaner inheritance and things like "super()". I'd like to be able to specify virtuality and non-virtuality.

Having a dog run and a program run? This does not make sense. Chewbacca lives on Endor, and this does not make sense. Therefore we must have more strongly-checked OO.

I started my OO life learning C++ and Java, grew to love Java, grew to hate Java, but it wouldn't be bad if Perl became a little more mainstream in it's OO. Just don't repeat Java's failings -- inconsistant API, lack of multiple inheritance, exception hell, etc. Perl already has a leg up since it has a much stronger base library -- so a little cleaner OO syntax to go with that library, that would be great.

My only OO heresy is that I despise "getter and setter" methods. Maybe that's not heresy, maybe those methods are against the spirit of message passing (I think they are). Anyhow, Perl doesn't have to mandate style as far as java does, where a programmer is cast down for making a public variable. It should, however, start to treat objects as though they have types.

Replies are listed 'Best First'.
Re^3: perl6 & OO
by hardburn (Abbot) on Feb 19, 2004 at 14:27 UTC

    I think this could be accomodated for in the oft-unused Perl message-signature feature, which right now only takes basic types.

    Apoc. 6 went through a total revamp of subroutines signatures. You can still use them like:

    sub foo { my ($bar, $baz) = @_; . . . }

    If you really want to, but if you did that, you'd be missing out on a lot of really cool stuff. I'm not so sure I personally want rigidly-enforced typing, but some of the signatures make calling conventions easier and make it obvious when we've called something incorrectly. At the same time, little flexibilty is lost over doing a complete check of @_ before we get to the actual code of the subroutine.

    Type systems applied to objects is one way to solve the problem of $dog->bark vs. $tree->bark, but I'm not sure if it's the best. Delegation and Traits (both widely talked about in Perl6 development) are potentially much better.

    My only OO heresy is that I despise "getter and setter" methods.

    Hardly heresy. Having lots of accessors and mutators (i.e., methods which provide direct access to private data) is a sign of poor class design. I won't go as far to say that they should be absolutely forbidden. Whenever I enter such a debate, I hear this Drunken Scottish Object Programmer saying:

    Ya can't just spread aaaccessors and muuutators all over your class design there ladie. You're screwwen the whole point!

    But then again, maybe I should get the voices in my head checked out.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Re: Re: perl6 & OO
by Ovid (Cardinal) on Feb 19, 2004 at 16:25 UTC
    For instance, if I have a "$program->run" and a "$dog->run" I don't want to be able to call $foo->run() and have it work with both dogs and programs.

    What's wrong with each providing a run method? If I'm iterating over a collection, I will not be putting both a program and a dog in the same collection. If I do, I probably have a serious confusion in my code. But what if I have a list of animals?

    foreach my $animal (@animals) { $animal->run; }

    That's what polymorphism is all about. Fortunately, that's very easy to do in Perl. Perl does provide many of the most useful OO features in Perl. What it lacks is easy to use encapsulation.

    As for getters and setters, can you think of any popular language with OO features that doesn't allow them? This isn't a Perl issue as far as I know. It's an issue with programmers treating objects as structs with methods calls. In fact, this is not a Bad Thing, so long as it's used appropriately.

    I think this could be accomodated for in the oft-unused Perl message-signature feature, which right now only takes basic types.

    Can you provide an example? I'm curious. If you're referring to prototypes (I don't think you are), then you should be aware that those are checked at compile time while method dispatch is resolved at runtime, thus making prototypes useless on method calls.

    Cheers,
    Ovid

    New address of my CGI Course.

      What's wrong with each providing a run method?

      Nothing. However, there is a LOT wrong with weakly typed variables and not requiring arguments to be of certain types, just because they all have 'run' methods does not mean I should be able to interchange programs and dogs. That's just asking for trouble.

      As for getters and setters, can you think of any popular language with OO features that doesn't allow them?

      I think you meant to say, is there any language that absolutely requires them? In which, no. The argument was in reference to prior comments of Perl OO's system being flexible. My comment was, essentially, as long as Perl doesn't do anything stupid like require getter/setter methods (it won't), I'm ok with a more rigid object structure.

      Can you provide an example?

      Dog::chewPantleg(Person ted) { # implementation }

      In the above example, you can't have a Dog chew another Dog's pantleg. It's enforced that the argument is a person.

      If you're referring to prototypes (I don't think you are)

      I am, however weak the current implementation is, that's what I'm talking about. Perl seems to allow OO implementation (a little better than how GTK might implement pseudo-OO in C), but it does not embrace it in spirit. I'm not saying it should be enforced, but it should be embraced.

      Then you should be aware that those are checked at compile time while method dispatch is resolved at runtime, thus making prototypes useless on method calls.

      I'd like to see that change in Perl6. Maybe it's a pipe dream, but it seems to be a requirement for clean polymorphism.

        What's wrong with each providing a run method?
        Nothing. However, there is a LOT wrong with weakly typed variables and not requiring arguments to be of certain types, just because they all have 'run' methods does not mean I should be able to interchange programs and dogs. That's just asking for trouble.
        This'll throw a runtime error for perl 6. If we can wedge it in, we may do it for perl 5 as well under Ponie, but I wouldn't hold your breath there.

        There's a chance that this might also throw a compile time error, but that's even less likely. Possibly a warning, but as there really isn't enough information at compile time to emit one with any reliability... probably not.