Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 02, 2008 int => struct cast | ||||
---|---|---|---|---|
| ||||
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 Re: int => struct cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: int => struct cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: int => struct cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: int => struct cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: int => struct cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | 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
|
Copyright © 1999-2021 by the D Language Foundation