split Error - no overload matches

Steven Schveighoffer schveiguy at gmail.com
Tue Feb 15 14:01:03 UTC 2022


On 2/14/22 5:18 PM, forkit wrote:
> On Monday, 14 February 2022 at 11:37:38 UTC, ag0aep6g wrote:
>> On 14.02.22 12:14, forkit wrote:
>>> However, if I uncomment the //import std.uni : isWhite;
>>>
>>> then it will compile.
>>>
>>> I don't understand. I thought 'import std;' would be sufficient here??
>>
>> "isWhite" is ambiguous. There's std.uni.isWhite and std.ascii.isWhite. 
>> `import std;` can't know which one you want.
> 
> thanks.
> 
> a little more help from the compiler itself would also have been 
> appreciated ;-)
> 
> e.g:
> 
> Error: The call to isWhite is ambiguous between the following methods: 
> 'std.uni.isWhite' and 'std.ascii.isWhite'

I'm kind of surprised more doesn't come out. Typically when a template 
doesn't match, it explains why it doesn't match. But for this case, it's 
not explaining anything.

First, the reason it doesn't match is due to D's limitations on how to 
match against "things that might compile". The definition for split that 
should accept this is:

```d
auto split(alias isTerminator, Range)(Range range)
if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(range.front))))
```

That second check is testing to see if it compiles, and if it doesn't 
for any reason, then it doesn't match.

Now, the symbol `isWhite` is an overload set, which is in error 
(ambiguous). So I'm not sure how to test this better.

Note, if I copy the entire definition of split locally, it explains the 
problem correctly (though still not as clear as it could be):

```
onlineapp.d(19): Error: template `test.split` cannot deduce function 
from argument types `!(isWhite)(string)`
onlineapp.d(6):        Candidate is: `split(alias isTerminator, 
Range)(Range range)`
   with `isTerminator = isWhite,
        Range = string`
   must satisfy the following constraint:
`       is(typeof(unaryFun!isTerminator(range.front)))`
```

And finally, I will note that split/splitter by default without any 
parameters splits on whitespace if that is your goal.

-Steve


More information about the Digitalmars-d-learn mailing list