in reply to Re: Constructor/Factory Orthodoxy
in thread Constructor/Factory Orthodoxy

When I write my $frob = new Frob (); I expect to get a Frob,

So do I! What I am more confused about is: Why I should reject a member of Sub_class_of_Frob as not a Frob?

Too much Perl. Not enough theory or practice with another language to broaden my mind.

Replies are listed 'Best First'.
Re: (Re:)+ Constructor/Factory Orthodoxy
by dws (Chancellor) on Feb 26, 2003 at 22:54 UTC
    Why I should reject a member of Sub_class_of_Frob as not a Frob?

    Because it violated the expectation I had that

    my $from = new Frob();
    would give me a Frob, not something that is a specialization of a Frob. One can argue about whether new warrants that expectation, but precendent across programming lanaguages does lean strongly that way.

      You are right that common usage in popular OO languages leans that way. As I stated I tend have that expectation myself. However:

      Can an abstract class not have a constructor?

      Should a subtype be able to be substituted for a supertype in any situation?

      Should I be concerning myself with my object's concrete type on the typical constructor call?

      When I consider these questions I wonder about the validity of my initial expectation.

      It depends on what the meaning of the word 'isa' is.

      Regarding:

      my $frob = new Frob();
      I have a greater expectation, in Perl, that Frob() is a routine call returning something here. That bothers me. This seems a very queer, obstrusive, distracting use of extraneous parentheses.

        That's because that should look like my $frob = Frob->new().

        (Before people flame me, Larry has said on many occasions that he wishes he had not introduced the indirect object syntax.)

        ------
        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.