Thread overview |
---|
December 14, 2019 Convert int[] to custom[] | ||||
---|---|---|---|---|
| ||||
``` alias custom = Typedef!int; custom[] arr = [1,2,3]; ``` Error: cannot implicitly convert expression [1, 2, 3] of type int[] to Typedef!(int, 0, null)[] ``` alias custom = Typedef!int; custom[] arr = cast(custom[])[1,2,3]; ``` Error: cannot cast expression 1 of type int to Typedef!(int, 0, null) Error: cannot cast expression 2 of type int to Typedef!(int, 0, null) Error: cannot cast expression 3 of type int to Typedef!(int, 0, null) Work only this variant: ``` alias custom = Typedef!int; custom[] arr = [cast(custom)1,cast(custom)2,cast(custom)3]; ``` but this crap |
December 14, 2019 Re: Convert int[] to custom[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to SR_team | On Saturday, 14 December 2019 at 10:41:14 UTC, SR_team wrote:
> Work only this variant:
> ```
> alias custom = Typedef!int;
> custom[] arr = [cast(custom)1,cast(custom)2,cast(custom)3];
> ```
> but this crap
import std.conv;
custom[] arr = [1,2,3].to!(custom[]);
OR
import std.algorithm;
import std.array;
custom[] arr = [1,2,3].map!(x => cast(custom)x).array;
work for me.
|
December 14, 2019 Re: Convert int[] to custom[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Saturday, 14 December 2019 at 12:38:18 UTC, JN wrote:
> On Saturday, 14 December 2019 at 10:41:14 UTC, SR_team wrote:
>> Work only this variant:
>> ```
>> alias custom = Typedef!int;
>> custom[] arr = [cast(custom)1,cast(custom)2,cast(custom)3];
>> ```
>> but this crap
>
> import std.conv;
> custom[] arr = [1,2,3].to!(custom[]);
>
> OR
>
> import std.algorithm;
> import std.array;
>
> custom[] arr = [1,2,3].map!(x => cast(custom)x).array;
>
>
> work for me.
thank
|
Copyright © 1999-2021 by the D Language Foundation