June 25, 2015 Range code and inference errors | ||||
|---|---|---|---|---|
| ||||
Hi everyone,
I've been doing quite a lot of range-based code lately and I've been bugged with an UX problem that's IMHO a real bummer for range usage to new users.
Take the example code:
```
import std.algorithm;
void main()
{
auto foo = [ "foo": "foo", "bar": "bar", "foobar": "foobar" ];
assert(foo.byKeyValue.all!((kvp) => kvp.Key == kvp.Value));
}
```
Quite straightforward, right ? Some of you might already have spotted the error, but here's what DMD think of it:
```
test.d(6): Error: template std.algorithm.searching.all cannot deduce function from argument types !((kvp) => kvp.Key == kvp.Value)(Result), candidates are:
/dlang/dmd-2.067.0/linux/bin64/../../src/phobos/std/algorithm/searching.d(100): std.algorithm.searching.all(alias pred = "a")
```
This happens because I got the delegate wrong. To get the right error, you have to know errors are gagged and so, let's remove the type inference error:
```
assert(foo.byKeyValue.all!((typeof(foo.byKeyValue.front) kvp) => kvp.Key == kvp.Value));
```
Which gives a proper error message:
```
test.d(6): Error: no property 'Key' for type 'Pair', did you mean 'key'?
test.d(6): Error: no property 'Value' for type 'Pair', did you mean 'value'?
```
Any change we can fix that ?
| ||||
June 25, 2015 Re: Range code and inference errors | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mathias Lang | On Thursday, 25 June 2015 at 13:27:23 UTC, Mathias Lang wrote:
> Hi everyone,
> I've been doing quite a lot of range-based code lately and I've been bugged with an UX problem that's IMHO a real bummer for range usage to new users.
>
> [...]
This is part of a bigger issue involving error messages for compilation errors due to not implementing a compile-interface such as InputRange. I've been meaning to write a DIP for this for a year now, I just need to find the time.
Atila
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply