Thread overview
Can't cast from void*
Feb 03, 2018
Kagamin
Feb 06, 2018
Kagamin
Feb 06, 2018
Temtaime
February 03, 2018
---
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?
February 05, 2018
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
February 06, 2018
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?
February 06, 2018
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
February 06, 2018
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.