Thread overview
int => struct cast
Oct 02, 2008
bearophile
Oct 02, 2008
bearophile
Oct 03, 2008
Alexander Pánek
Oct 03, 2008
Max Samukha
Oct 03, 2008
bearophile
October 02, 2008
Why can't I cast  an n int to a struct with only a x int field?
Time ago someone has said that all internal errors are problems, is this known and bad?

struct S { int x; }
void main() {
    int n = 1;
    int[S] a;
    a[ cast(S)n ]++;
}

OUTPUT DMD 1.035:
temp.d(5): Error: e2ir: cannot cast from int to S
Internal error: e2ir.c 3904

Bye,
bearophile
October 02, 2008
On Thu, Oct 2, 2008 at 10:04 AM, bearophile <bearophileHUGS@lycos.com> wrote:
> Why can't I cast  an n int to a struct with only a x int field?
> Time ago someone has said that all internal errors are problems, is this known and bad?
>
> struct S { int x; }
> void main() {
>    int n = 1;
>    int[S] a;
>    a[ cast(S)n ]++;
> }
>
> OUTPUT DMD 1.035:
> temp.d(5): Error: e2ir: cannot cast from int to S
> Internal error: e2ir.c 3904
>
> Bye,
> bearophile
>

I'm guessing you can't do it because it doesn't make any sense.  Since when did you expect this to work?  The typical solution is to cast through a pointer, i.e. "*cast(S*)&n".

And yes, any time you see "internal error" put.  it.  in.  bugzilla.  please.
October 02, 2008
Jarrett Billingsley:
> I'm guessing you can't do it because it doesn't make any sense.  Since when did you expect this to work?  The typical solution is to cast through a pointer, i.e. "*cast(S*)&n".

The typical D solution here is probably:
a[ S(n) ]++;

Bye and thank you,
bearophile
October 03, 2008
bearophile wrote:
> Why can't I cast  an n int to a struct with only a x int field?

Why would you want to? This makes no sense.
October 03, 2008
On Thu, 02 Oct 2008 10:04:18 -0400, bearophile <bearophileHUGS@lycos.com> wrote:

>Why can't I cast  an n int to a struct with only a x int field?
>Time ago someone has said that all internal errors are problems, is this known and bad?
>
>struct S { int x; }
>void main() {
>    int n = 1;
>    int[S] a;
>    a[ cast(S)n ]++;
>}
>
>OUTPUT DMD 1.035:
>temp.d(5): Error: e2ir: cannot cast from int to S
>Internal error: e2ir.c 3904
>
>Bye,
>bearophile

There is a not well documented feature that turns cast(S)n into S(n),
if opCall is defined

struct S {
  int x;
   static S opCall(int x) { S r; r.x = x; return r; }
}

void main() {
    int n = 1;
    int[S] a;
    a[ cast(S)n ]++;
}
October 03, 2008
Max Samukha:
> There is a not well documented feature that turns cast(S)n into S(n),
> if opCall is defined

Cute, and unknown to me.

Thank you,
bearophile