Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-10 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/9/21 3:20 PM, Q. Schroll wrote: Yes. Actually, I need it for slices, but only pointer part of it really mattered. A Derived[] is implicitly a const(Base)[], not a Base[]. A void delegate() @safe[] is implicitly a const(void delegate())[]. But it seems a void function() @safe[] **isn't** im

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-10 Thread sighoya via Digitalmars-d-learn
On Saturday, 9 January 2021 at 18:16:04 UTC, Q. Schroll wrote: This makes no sense in my head. Is there some reason I'm unable to see? And yes, I think it should work likewise for function pointers, too.

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-10 Thread sighoya via Digitalmars-d-learn
On Sunday, 10 January 2021 at 00:44:26 UTC, Q. Schroll wrote: Car[] cars = ...; Vehicle[] vehicles = cars; vehicles[0] = new Boat; // err... // cars[0] is a Boat now!? Now what? Do what Java does and throw an Exception? Or use const to prevent mutation? Yes, you are right: ``` Deri

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 January 2021 at 21:57:43 UTC, sighoya wrote: On Saturday, 9 January 2021 at 20:20:38 UTC, Q. Schroll wrote: That's not what I mean. You copy the reference. That's not what referencing meant. Derived d = new Derived(); Base* bp = &d; // fails const(Base) cbp = &d; // compil

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread sighoya via Digitalmars-d-learn
On Saturday, 9 January 2021 at 20:20:38 UTC, Q. Schroll wrote: That's not what I mean. You copy the reference. That's not what referencing meant. Derived d = new Derived(); Base* bp = &d; // fails const(Base) cbp = &d; // compiles. Generally, allowing covariant assignment for the Ptr, i

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 January 2021 at 20:00:35 UTC, Jacob Carlborg wrote: On 2021-01-09 19:16, Q. Schroll wrote: Say I have a class hierarchy like this:   class Base { }   class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense t

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Jacob Carlborg via Digitalmars-d-learn
On 2021-01-09 19:16, Q. Schroll wrote: Say I have a class hierarchy like this:   class Base { }   class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense to me. It can: Base b = new Derived(); One can replace Base by a

[Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
Say I have a class hierarchy like this: class Base { } class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense to me. One can replace Base by a @system delegate type (SysDG) and Derived by a @safe delegate type (SafeDG)