Re: Should this always work?

2021-05-04 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Tuesday, 4 May 2021 at 14:18:30 UTC, Ola Fosheim Grøstad wrote: On Tuesday, 4 May 2021 at 13:58:59 UTC, Steven Schveighoffer wrote: Yeah, I wasn't aware of the more general usage, I thought it was always a pointer adjustment. But I also am not steeped in the terminology, just parroting what

Re: Should this always work?

2021-05-04 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Tuesday, 4 May 2021 at 13:58:59 UTC, Steven Schveighoffer wrote: Yeah, I wasn't aware of the more general usage, I thought it was always a pointer adjustment. But I also am not steeped in the terminology, just parroting what I've heard. My understanding is that a thunk is the code object

Re: Should this always work?

2021-05-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/4/21 6:03 AM, Ola Fosheim Grøstad wrote: On Tuesday, 4 May 2021 at 01:20:15 UTC, Q. Schroll wrote: On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote: On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote: I always thought as long as an object implements an interface, it should be

Re: Should this always work?

2021-05-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/4/21 9:21 AM, Paul Backus wrote: On Tuesday, 4 May 2021 at 10:21:42 UTC, Ola Fosheim Grøstad wrote: On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer wrote: An interface cast involves a thunk (constant pointer adjustment) to get to the interface/object Yes, but it isn't a

Re: Should this always work?

2021-05-04 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 4 May 2021 at 01:20:15 UTC, Q. Schroll wrote: On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote: No. An interface is like a pointer to a pointer. Can you elaborate on this one? I don't really get it. Is an object handle also like a pointer to a pointer? (I feel like I

Re: Should this always work?

2021-05-04 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 4 May 2021 at 10:21:42 UTC, Ola Fosheim Grøstad wrote: On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer wrote: An interface cast involves a thunk (constant pointer adjustment) to get to the interface/object Yes, but it isn't a https://en.wikipedia.org/wiki/Thunk ?

Re: Should this always work?

2021-05-04 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer wrote: An interface cast involves a thunk (constant pointer adjustment) to get to the interface/object Yes, but it isn't a https://en.wikipedia.org/wiki/Thunk ?

Re: Should this always work?

2021-05-04 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
On Tuesday, 4 May 2021 at 01:20:15 UTC, Q. Schroll wrote: On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote: On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote: I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really

Re: Should this always work?

2021-05-03 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote: On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote: I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really points to a supporting object. No. An interface is like a

Re: Should this always work?

2021-05-01 Thread frame via Digitalmars-d-learn
On Saturday, 1 May 2021 at 15:52:52 UTC, Mike Parker wrote: No instance is being created by the cast. The instance already exists. The cast just allows you to treat the pointer to the instance as a specific type. It’s no different than casting from C to A really. You just have the void* as an

Re: Should this always work?

2021-05-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/1/21 12:55 AM, frame wrote: I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really points to a supporting object. I have the similar structure: ```d interface AI {    string doSomething(); } template S() {    void foo() {

Re: Should this always work?

2021-05-01 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 1 May 2021 at 15:21:28 UTC, frame wrote: btw: why is it even allowed to create an instance via cast as an abstract thing? No instance is being created by the cast. The instance already exists. The cast just allows you to treat the pointer to the instance as a specific type.

Re: Should this always work?

2021-05-01 Thread frame via Digitalmars-d-learn
On Saturday, 1 May 2021 at 10:08:17 UTC, Mike Parker wrote: So the pitfall is the same: when you're casting class references to and from pointers, it's up to you to ensure that the type you cast to is appropriate. You get no help from the compiler here. I see, this can be dangerous - yet,

Re: Should this always work?

2021-05-01 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 1 May 2021 at 08:03:45 UTC, frame wrote: In case of a void* to abstract class casting, I assume the compiler just inserts the right vtable, so this cast is unproblematic - or is there also a pitfall? I wouldn't expect there to be any consideration of vtables whatsoever. The

Re: Should this always work?

2021-05-01 Thread frame via Digitalmars-d-learn
On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote: That only works when you're casting one class/interface reference to another. It doesn't > > work when you're casting raw pointers. Thanks for clarification. In case of a void* to abstract class casting, I assume the compiler

Re: Should this always work?

2021-05-01 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote: I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really points to a supporting object. No. An interface is like a pointer to a pointer. So to get to the class, you have to go

Should this always work?

2021-04-30 Thread frame via Digitalmars-d-learn
I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really points to a supporting object. I have the similar structure: ```d interface AI { string doSomething(); } template S() { void foo() { } } abstract class A : AI