D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12365 - Assigning 1D array to 2D array should require cast
Summary: Assigning 1D array to 2D array should require cast
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-14 16:40 UTC by growlercab
Modified: 2022-08-22 12:51 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description growlercab 2014-03-14 16:40:54 UTC
The code in Ex.1 compiles and runs fine but I think it needs a cast:
---
// Ex.1
void main()
{
    int[] a=[1,2,3,4];
    int[2][2] b = a;
}
---


Array assignment in Ex.2 only works with a cast:
---
// Ex.2
void main()
{
    int[] a=[1,2,3,4];
    int[2][2] b;

    b = a; // Compile time error

    cast(int[4])b = a; // works ok
}
---

I think it should be consistent, preferably with a cast.


Thanks,
ed
Comment 1 growlercab 2014-03-14 16:49:41 UTC
I'm not sure how to do the cast on initialisation though:
---
// Ex.1
void main()
{
    int[] a=[1,2,3,4];
    int[2][2] b = a;
}
---

Would it be:
    int[2][2] b = cast(int[2][2])a;

but it does not match the existing casts required for assignment:    

    cast(int[4])b = a;

Maybe just disallow code as per Ex.1 and only allow the cast assignment from Ex.2?


Thanks,
ed
Comment 2 RazvanN 2022-08-22 12:51:41 UTC
The code in Ex1 now yields:

test.d(4): Error: cannot implicitly convert expression `a` of type `int[]` to `int[2][]`

Closing as fixed.