Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-07-02 09:14:37


----- Original Message -----
From: "Douglas Gregor" <gregod_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, July 02, 2002 9:52 AM
Subject: Re: [boost] Re: Re: Yet another Variant class

> On Tuesday 02 July 2002 04:35 am, David Abrahams wrote:
> > > textbooks would tell us to write the object hierarchy like this:
> > >
> > > class Expression { /* ... */ };
> > > class Plus : public Expression { /* ... */ };
> > > class Minus : public Expression { /* ... */ };
> > > class Literal : public Expression { /* ... */ };
> > >
> > > Why? Because Plus is-a(n) Expression, and a Literal is-a(n)
Expression.
> >
> > 'xcept it ain't. There is no is-a relationship with variant. If there
was,
> > I'd be able to pass a double* where an expression* was expected.
>
> Do you really want to limit the definition of 'is-a' to C++ inheritance?

Not neccessarily.

> That
> would say that dynamic polymorphism is impossible via any other
mechanism. I
> would say that:
>
> struct some_type {
> void visit_me(const boost::function<void, some_type*>& visitor);
> };
>
> is every bit as polymorphic as:
>
> struct visitor_interface {
> virtual void visit(some_type*) = 0;
> };
>
> struct some_type {
> void visit_me(visitor_interface* visitor);
> };
>
> but you wouldn't agree?

This is getting awfully tangled. We weren't discussing polymorphism in
general, but is-a relationships. Where's the is-a reltionship here?

> > Structurally speaking you've got it inside-out, in fact. Given an
> > expression lvalue, we can get a double lvalue.
>
> Given a double, we can build an expression from it (derived-to-base
cast).

Bad analogy. Derived-to-base never requires the construction of a new
object. That's like saying vector<double> is-a int because I can write

    vector<double> x(300);

> Given an expression that is really a double

Bad premise. Your Expressions are never doubles. They can contain doubles.
Type identity is important in C++.

> we can get to the double
> (base-to-derived cast). What's inside-out?

That's much more-analogous to derived-to-base than base-to-derived. The
double storage exists within the Expression object, just as the Base
storage exists within the Derived object. That's what's inside-out.

> > > PyObject*, variant, any -- they're all just union types. Is a union a
> > > container of one?
> >
> > Yes! A union definitely does *not* present a logical is-a relationship.
> >
> > -Dave
>
> A C-style union is a bad example (at least, for my side of the fence)
because
> it feels syntactically like a container. If we step away from C++: would
you
> say that the is-a relationship holds in an ML variant? e.g.,
>
> type expression = Literal of float
> | Plus of expression * expression
> | Minus of expression * expression
>

I don't have enough of a feel for ML to comment, having never used it
myself. Fortunately, we're discussing C++ <0.2 wink>

> Does it hold that Plus(x, y) is-a expression?

Which definition of Plus/expression are you using?

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk