Thread overview
'cast(Base )(derived)' is not an lvalue
Jun 01, 2004
Kris
Jun 08, 2004
Walter
Jun 08, 2004
Kris
Jun 08, 2004
Kris
Jun 08, 2004
Walter
June 01, 2004
Here's an opApply() with a problem: I wish to iterate over a list of derived
object instances, but pass each of them to the opApply() delegate as the
public (base class) instead.

However, invoking the delegate with the derived class produces a
compile-time error as follows: "'cast(Base )(derived)' is not an lvalue"

Note that the derived class is deliberately private (to hide implementation) so I don't want to expose it at all. I can get around the problem by creating a temporary Base variable and manually assigning it to derived, before passing that to the delegate. On the face of it, this appears to be an expression evaluation issue ...

class Base
{
        abstract void foo();
}

private class Derived : Base
{
        private Derived next;

        void foo(){}
}

class Test
{
        private Derived root;

        int opApply (int delegate(inout Base) dg)
        {
                int result = 0;
                Derived derived = root;

                while (derived)
                      {
                      // compile-time error here ...
                      if ((result = dg (derived)) != 0)
                           break;
                      derived = derived.next;
                      }
                return result;
        }
}


June 08, 2004
The issue here is the 'inout' on the argument. Allowing such an implicit cast for an inout opens a huge type safety hole. The caller would know the type as derived, however, it would be Base.

"Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c9ic4p$1knk$1@digitaldaemon.com...
> Here's an opApply() with a problem: I wish to iterate over a list of
derived
> object instances, but pass each of them to the opApply() delegate as the
> public (base class) instead.
>
> However, invoking the delegate with the derived class produces a
> compile-time error as follows: "'cast(Base )(derived)' is not an lvalue"
>
> Note that the derived class is deliberately private (to hide
implementation)
> so I don't want to expose it at all. I can get around the problem by creating a temporary Base variable and manually assigning it to derived, before passing that to the delegate. On the face of it, this appears to be an expression evaluation issue ...
>
> class Base
> {
>         abstract void foo();
> }
>
> private class Derived : Base
> {
>         private Derived next;
>
>         void foo(){}
> }
>
> class Test
> {
>         private Derived root;
>
>         int opApply (int delegate(inout Base) dg)
>         {
>                 int result = 0;
>                 Derived derived = root;
>
>                 while (derived)
>                       {
>                       // compile-time error here ...
>                       if ((result = dg (derived)) != 0)
>                            break;
>                       derived = derived.next;
>                       }
>                 return result;
>         }
> }
>
>


June 08, 2004
That may be so, but an explicit cast is not allowed either. Currently, the only way to do this is to assign a temporary variable as described below. That's kinda' klutzy though; certainly not very elegant <g>

Any other method to work around this?

- Kris


"Walter" <newshound@digitalmars.com> wrote in message news:ca4pg1$1tfs$3@digitaldaemon.com...
> The issue here is the 'inout' on the argument. Allowing such an implicit cast for an inout opens a huge type safety hole. The caller would know the type as derived, however, it would be Base.
>
> "Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c9ic4p$1knk$1@digitaldaemon.com...
> > Here's an opApply() with a problem: I wish to iterate over a list of
> derived
> > object instances, but pass each of them to the opApply() delegate as the
> > public (base class) instead.
> >
> > However, invoking the delegate with the derived class produces a
> > compile-time error as follows: "'cast(Base )(derived)' is not an lvalue"
> >
> > Note that the derived class is deliberately private (to hide
> implementation)
> > so I don't want to expose it at all. I can get around the problem by creating a temporary Base variable and manually assigning it to derived, before passing that to the delegate. On the face of it, this appears to
be
> > an expression evaluation issue ...
> >
> > class Base
> > {
> >         abstract void foo();
> > }
> >
> > private class Derived : Base
> > {
> >         private Derived next;
> >
> >         void foo(){}
> > }
> >
> > class Test
> > {
> >         private Derived root;
> >
> >         int opApply (int delegate(inout Base) dg)
> >         {
> >                 int result = 0;
> >                 Derived derived = root;
> >
> >                 while (derived)
> >                       {
> >                       // compile-time error here ...
> >                       if ((result = dg (derived)) != 0)
> >                            break;
> >                       derived = derived.next;
> >                       }
> >                 return result;
> >         }
> > }
> >
> >
>
>


June 08, 2004
"Walter"  wrote:
> The issue here is the 'inout' on the argument. Allowing such an implicit cast for an inout opens a huge type safety hole. The caller would know the type as derived, however, it would be Base.

Hmmm ... I think I misunderstood your reply the first time around Walter. The caller is not expecting to see the Derived type at all (because it's completely private); only the Base class is visible. Hence the

int opApply (int delegate(inout Base) dg)

To restate the original problem: I'm trying to pass a Derived class as a Base implementation to the opApply() delegate. Your reply appears to indicate that I was attempting the reverse?

- Kris


> "Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c9ic4p$1knk$1@digitaldaemon.com...
> > Here's an opApply() with a problem: I wish to iterate over a list of
> derived
> > object instances, but pass each of them to the opApply() delegate as the
> > public (base class) instead.
> >
> > However, invoking the delegate with the derived class produces a
> > compile-time error as follows: "'cast(Base )(derived)' is not an lvalue"
> >
> > Note that the derived class is deliberately private (to hide
> implementation)
> > so I don't want to expose it at all. I can get around the problem by creating a temporary Base variable and manually assigning it to derived, before passing that to the delegate. On the face of it, this appears to
be
> > an expression evaluation issue ...
> >
> > class Base
> > {
> >         abstract void foo();
> > }
> >
> > private class Derived : Base
> > {
> >         private Derived next;
> >
> >         void foo(){}
> > }
> >
> > class Test
> > {
> >         private Derived root;
> >
> >         int opApply (int delegate(inout Base) dg)
> >         {
> >                 int result = 0;
> >                 Derived derived = root;
> >
> >                 while (derived)
> >                       {
> >                       // compile-time error here ...
> >                       if ((result = dg (derived)) != 0)
> >                            break;
> >                       derived = derived.next;
> >                       }
> >                 return result;
> >         }
> > }
> >
> >
>
>


June 08, 2004
The 'inout' means you can change the caller's reference from a Derived to *any other class* derived from Base. This is a huge type error.

"Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:ca50ds$29qa$1@digitaldaemon.com...
> "Walter"  wrote:
> > The issue here is the 'inout' on the argument. Allowing such an implicit cast for an inout opens a huge type safety hole. The caller would know
the
> > type as derived, however, it would be Base.
>
> Hmmm ... I think I misunderstood your reply the first time around Walter. The caller is not expecting to see the Derived type at all (because it's completely private); only the Base class is visible. Hence the
>
> int opApply (int delegate(inout Base) dg)
>
> To restate the original problem: I'm trying to pass a Derived class as a Base implementation to the opApply() delegate. Your reply appears to indicate that I was attempting the reverse?
>
> - Kris
>
>
> > "Kris" <someidiot@earthlink.dot.dot.dot.net> wrote in message news:c9ic4p$1knk$1@digitaldaemon.com...
> > > Here's an opApply() with a problem: I wish to iterate over a list of
> > derived
> > > object instances, but pass each of them to the opApply() delegate as
the
> > > public (base class) instead.
> > >
> > > However, invoking the delegate with the derived class produces a
> > > compile-time error as follows: "'cast(Base )(derived)' is not an
lvalue"
> > >
> > > Note that the derived class is deliberately private (to hide
> > implementation)
> > > so I don't want to expose it at all. I can get around the problem by creating a temporary Base variable and manually assigning it to
derived,
> > > before passing that to the delegate. On the face of it, this appears
to
> be
> > > an expression evaluation issue ...
> > >
> > > class Base
> > > {
> > >         abstract void foo();
> > > }
> > >
> > > private class Derived : Base
> > > {
> > >         private Derived next;
> > >
> > >         void foo(){}
> > > }
> > >
> > > class Test
> > > {
> > >         private Derived root;
> > >
> > >         int opApply (int delegate(inout Base) dg)
> > >         {
> > >                 int result = 0;
> > >                 Derived derived = root;
> > >
> > >                 while (derived)
> > >                       {
> > >                       // compile-time error here ...
> > >                       if ((result = dg (derived)) != 0)
> > >                            break;
> > >                       derived = derived.next;
> > >                       }
> > >                 return result;
> > >         }
> > > }
> > >
> > >
> >
> >
>
>