www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how convert the range to slice ?

reply "mzfhhhh" <mzfhhhh foxmail.com> writes:
is there any simple way to convert?

int [] arr1 = [1,2,3].map!"a*2";   //compile error
int [] arr2 = [1,2,3].filter!"a<3";//compile error
Jan 28 2015
parent reply "data man" <datamanrb gmail.com> writes:
On Wednesday, 28 January 2015 at 14:32:38 UTC, mzfhhhh wrote:
 is there any simple way to convert?

 int [] arr1 = [1,2,3].map!"a*2";   //compile error
 int [] arr2 = [1,2,3].filter!"a<3";//compile error
auto arr1 = [1,2,3].map!"a*2".array; auto arr2 = [1,2,3].filter!"a<3".array;
Jan 28 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Wednesday, 28 January 2015 at 14:54:27 UTC, data man wrote:
 auto arr1 = [1,2,3].map!"a*2".array;
 auto arr2 = [1,2,3].filter!"a<3".array;
Is there any chance we could add logic to dmd+phobos that hints user about this?
Jan 28 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Nordlöw:

 Is there any chance we could add logic to dmd+phobos that hints 
 user about this?
It's such a fundamental part of D+Phobos that newbies are forced to learn this quickly. On the other hand an informative error message could be useful... What error message do you suggest? Bye, bearophile
Jan 28 2015
next sibling parent reply "Chris Williams" <yoreanon-chrisw yahoo.co.jp> writes:
On Wednesday, 28 January 2015 at 22:43:36 UTC, bearophile wrote:
 Nordlöw:

 Is there any chance we could add logic to dmd+phobos that 
 hints user about this?
It's such a fundamental part of D+Phobos that newbies are forced to learn this quickly. On the other hand an informative error message could be useful... What error message do you suggest? Bye, bearophile
Range is not castable to array. See std.array.array to generate an array from a Range.
Jan 28 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Chris Williams:

 Range is not castable to array. See std.array.array to generate 
 an array from a Range.
Currently this program: void main() { import std.range; int[] a = iota(10); } Gives an error like: test.d(3,19): Error: cannot implicitly convert expression (iota(10)) of type Result to int[] For the error message to be like yours, the compiler has to recognize a Range, this could be possible because foreach() already does that. I think you can try to open a diagnostic enhancement request. In D there are already examples of hardcoded error messages targeting newbies: void main() { int x; writeln(a); printf("%d\n", x); } Gives: test.d(3,5): Error: 'writeln' is not defined, perhaps you need to import std.stdio; ? test.d(4,5): Error: 'printf' is not defined, perhaps you need to import core.stdc.stdio; ? Bye, bearophile
Jan 28 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Wednesday, 28 January 2015 at 23:15:32 UTC, bearophile wrote:
 I think you can try to open a diagnostic enhancement request.
Nice, that's what I'd hoped for you'd say :) I'll dig into it later on...
Jan 28 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Wednesday, 28 January 2015 at 23:21:34 UTC, Nordlöw wrote:
 I'll dig into it later on...
Is started digging a bit... The magic happens at line 103 in cast.c. How do I most conveniently figure out which members (functions) a type (e->type) has? I figured I could check for typical InputRange members and issue a hint about using .array if e->type has them.
Feb 01 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Nordlöw:

 Is started digging a bit...

 The magic happens at line 103 in cast.c.

 How do I most conveniently figure out which members (functions) 
 a type (e->type) has?

 I figured I could check for typical InputRange members and 
 issue a hint about using .array if e->type has them.
It's probably better to ask such questions on GitHub (and to open an enhancement request in Bugzilla). Bye, bearophile
Feb 02 2015
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Monday, 2 February 2015 at 16:56:02 UTC, bearophile wrote:
 Nordlöw:

 Is started digging a bit...

 The magic happens at line 103 in cast.c.

 How do I most conveniently figure out which members 
 (functions) a type (e->type) has?

 I figured I could check for typical InputRange members and 
 issue a hint about using .array if e->type has them.
It's probably better to ask such questions on GitHub (and to open an enhancement request in Bugzilla). Bye, bearophile
I cracked it. First proof of concept here https://github.com/nordlow/dmd/commit/40ce0ecf34f90c4d3053c47e9286d7574f596e15
Feb 02 2015
parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Monday, 2 February 2015 at 19:04:52 UTC, Nordlöw wrote:
https://github.com/nordlow/dmd/commit/40ce0ecf34f90c4d3053c47e9286d7574f596e15

Made it PR at

https://github.com/D-Programming-Language/dmd/pull/4371
Feb 02 2015
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Wednesday, 28 January 2015 at 22:43:36 UTC, bearophile wrote:
 It's such a fundamental part of D+Phobos that newbies are 
 forced to learn this quickly. On the other hand an informative 
 error message could be useful...

 What error message do you suggest?
Something like: ..., expression of type (SomeRange!T) must be converted/transformed to T[] through .array. I have no idea if DMD could figure this out by intercepting array assignment somehow. DMD must at least be aware of the Range concept throught its duck type members when generating code for foreach, right?
Jan 28 2015