www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't cast from void*

reply Kagamin <spam here.lot> writes:
---
interface A{}

void* a=cast(void*)5;
A b=cast(A)a; //ok
A c=cast(A)cast(void*)5; //error
---
Last line gives Error: cannot cast `void*` to `A`. Is it intended?
Feb 03
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/3/18 12:37 PM, Kagamin wrote:
 ---
 interface A{}
 
 void* a=cast(void*)5;
 A b=cast(A)a; //ok
 A c=cast(A)cast(void*)5; //error
 ---
 Last line gives Error: cannot cast `void*` to `A`. Is it intended?
Superficially, it looks like it should work, given the previous 2 lines. But I don't know what the compiler does to convince itself your code is wrong when you do it all in one expression. Is there a more pragmatic use case why this should be possible? It would help give motivation for fixing the problem. Clearly, `5` isn't going to be a valid interface pointer. -Steve
Feb 05
parent reply Kagamin <spam here.lot> writes:
On Monday, 5 February 2018 at 15:33:02 UTC, Steven Schveighoffer 
wrote:
 Is there a more pragmatic use case why this should be possible?
Maybe for least surprise. The error message almost convinced me that such cast is impossible, only because of my memory that this cast used to be possible kept me trying. Is 5 not good because it's not big enough?
Feb 06
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/6/18 3:29 AM, Kagamin wrote:
 On Monday, 5 February 2018 at 15:33:02 UTC, Steven Schveighoffer wrote:
 Is there a more pragmatic use case why this should be possible?
Maybe for least surprise. The error message almost convinced me that such cast is impossible, only because of my memory that this cast used to be possible kept me trying. Is 5 not good because it's not big enough?
Honestly, I don't know why it's not working. But maybe there is a reason (i.e. it's not a bug and was done on purpose). All I meant is that, if you have a more practical correct reason for casting an integer literal to an interface, then we can show that it's worth worrying about. By illustration of what I'm talking about, D complains about this: int * foo() { int x; return &x; } But not this: int * bar() { int x; int *p = &x; return p; } Complaining about not being able to cast 5 directly to an interface, when you can do it in 2 statements, may be like complaining that bar compiles, why not foo? -Steve
Feb 06
prev sibling parent Temtaime <temtaime gmail.com> writes:
On Tuesday, 6 February 2018 at 08:29:05 UTC, Kagamin wrote:
 On Monday, 5 February 2018 at 15:33:02 UTC, Steven 
 Schveighoffer wrote:
 Is there a more pragmatic use case why this should be possible?
Maybe for least surprise. The error message almost convinced me that such cast is impossible, only because of my memory that this cast used to be possible kept me trying. Is 5 not good because it's not big enough?
Fill a bugreport.
Feb 06