digitalmars.D.learn - int => struct cast
- bearophile <bearophileHUGS lycos.com> Oct 02 2008
- "Jarrett Billingsley" <jarrett.billingsley gmail.com> Oct 02 2008
- bearophile <bearophileHUGS lycos.com> Oct 02 2008
- =?UTF-8?B?QWxleGFuZGVyIFDDoW5law==?= Oct 03 2008
- Max Samukha <samukha voliacable.com.removethis> Oct 03 2008
- bearophile <bearophileHUGS lycos.com> Oct 03 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
Oct 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.
Oct 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
Oct 02 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.
Oct 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 ]++; }
Oct 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
Oct 03 2008









bearophile <bearophileHUGS lycos.com> 