Jump to page: 1 2
Thread overview
how convert the range to slice ?
Jan 28, 2015
mzfhhhh
Jan 28, 2015
data man
Jan 28, 2015
Nordlöw
Jan 28, 2015
bearophile
Jan 28, 2015
Chris Williams
Jan 28, 2015
bearophile
Jan 28, 2015
Nordlöw
Feb 01, 2015
Nordlöw
Feb 02, 2015
bearophile
Feb 02, 2015
Nordlöw
Feb 02, 2015
Nordlöw
Jun 09, 2016
Nordlöw
Jan 28, 2015
Nordlöw
January 28, 2015
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
January 28, 2015
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;
January 28, 2015
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?
January 28, 2015
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
January 28, 2015
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.
January 28, 2015
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
January 28, 2015
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?
January 28, 2015
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...
February 01, 2015
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.
February 02, 2015
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
« First   ‹ Prev
1 2