in reply to OO: Leaving a constructor midway?

This is all about the way you think.

To be frank, I see this particular practice as non-OO, although it is inside an OO prorgam. (The rest of your code could be quite OO, I haven't seen it, and I have no right to comment it.)

Constructor is used to create the object, make it EXIST. Before you exit the constructor, this object does not exist in the OO world. Now how can something start to ACT before it even EXIST?

On the other hand, if you make it act inside the constructor, then what is the point to do it in a OO-way, becaue it is actually really procedural.

Replies are listed 'Best First'.
Re: OO: Leaving a constructor midway?
by Abigail-II (Bishop) on Oct 22, 2003 at 08:16 UTC
    Well, in Perl the constructor is called bless. Now, it's popular to call a subroutine that is called as a class method, and that returns a blessed reference a 'constructor', but on the language level, it's just a subroutine that's called as a class method. Nothing special there.

    And, as far as I know, it's not possible to leave a bless midway. From a language perspective, it's an atomic operation, with one op in the optree.

    Of course, in the given code fragment, neither bless, nor the subroutine returning the blessed reference is left halfway. Printing out a redirection header won't make your program exit (unless there are some bugs somewhere).

    Abigail

      (I'm going to regret doing this, but what the hey!)

      The magicking action in Perl may be bless. But, OOD/OOP are language-independent concepts. The fact that Perl has certain quirks in how it implements OO (as opposed to the C++ or Java quirks) should be irrelevant to a discussion of OO technique.

      The "popular way" of using a subroutine called as a class method that returns a blessed reference also happens to be a rather decent implementation of an OO design for constructing an object. Your statement about "on the language level" is irrelevant to the discussion. On the language level, C++ reuses a ton of C features that are not OO. So does Perl. So what?!?

      OP is calling some class method sub (presumably named new) as a constructor. His program calls it, probably with some set of values, and gets back something that looks, talks, and walks like an object. Sounds like a constructor to me? The fact that, under the hood, it calls bless is completely irrelevant to the client code. In fact, a number of programmers use OO Perl without ever knowing about bless.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        my $snickle = CGI -> new; package Gonbagger; sub one {bless [] => "Fumble"} sub two {one} sub three {two} sub four {$snickle}
        All four subs return a blessed object. Do you call all of them constructors? Would you call an accessor method that happens to return an object (instead of say a string or a number) a constructor? After all, the returned value is something that "looks, talks, and walks like an object." Client code doesn't know what's going on under the hood.

        Java and Ruby have methods that are constructors. They are special, in the sense that they are called implicitely, and can't/shouldn't be called directly. Unlike Perl's class methods that just happen to return an object.

        What if I write my class like this:

        package Lioger; use Exporter (); @Lioger::EXPORT = qw /lioger1/; @Lioger::ISA = qw /Exporter/; sub lioger1 { return if @_; my $x = bless [] => __PACKAGE__; $x [0] = $x; } sub lioger2 { ${$_ [0]} [0] }
        Are both methods constructors?

        Language like Java, Ruby and C++ don't call just any function that happens to return an object a "constructor".

        Abigail