digitalmars.D.bugs - 'cast(Base )(derived)' is not an lvalue
- Kris (36/36) Jun 01 2004 Here's an opApply() with a problem: I wish to iterate over a list of der...
- Walter (7/43) Jun 08 2004 The issue here is the 'inout' on the argument. Allowing such an implicit
- Kris (8/61) Jun 08 2004 That may be so, but an explicit cast is not allowed either. Currently, t...
- Kris (10/63) Jun 08 2004 Hmmm ... I think I misunderstood your reply the first time around Walter...
- Walter (9/74) Jun 08 2004 The 'inout' means you can change the caller's reference from a Derived t...
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; } }
Jun 01 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 ofderivedobject 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 hideimplementation)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; } }
Jun 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...beHere's an opApply() with a problem: I wish to iterate over a list ofderivedobject 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 hideimplementation)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 toan 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; } }
Jun 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...beHere's an opApply() with a problem: I wish to iterate over a list ofderivedobject 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 hideimplementation)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 toan 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; } }
Jun 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:theThe 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 knowthetype 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 ofderivedobject instances, but pass each of them to the opApply() delegate aslvalue"public (base class) instead. However, invoking the delegate with the derived class produces a compile-time error as follows: "'cast(Base )(derived)' is not anderived,Note that the derived class is deliberately private (to hideimplementation)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 totobefore passing that to the delegate. On the face of it, this appearsbean 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; } }
Jun 08 2004