How the hell to split multiple delims?

AlphaPurned Alpha at Beta.com
Sun Feb 16 09:57:26 UTC 2020


On Saturday, 15 February 2020 at 14:35:59 UTC, Steven 
Schveighoffer wrote:
> On 2/15/20 6:32 AM, AlphaPurned wrote:
>> I've tried 10 different ways with split and splitter, I've 
>> used all the stuff that people have said online but nothing 
>> works. I always get a template mismatch error.
>> 
>> Why is something so easy to do so hard in D?
>> 
>> auto toks = std.regex.split(l, Regex("s"));
>> auto toks = std.regex.splitter(l, Regex("s"));
>> auto toks = std.regex.splitter(l, ctRegex!r"\.");
>
> What type is 'l'?
>
> -Steve

l happened to be a char ;/

I had

auto s = ...;

...

foreach(l; s)

     splitter(l,...)

and I was thinking s was an array but it happened to be a string 
so l was char.

The error message made me think(because I was expecting it to 
just work) it was with the regex since I remember some issues 
with regex and I haven't used them in a while so I thought maybe 
I was doing something wrong.

It would be far better if the error message just told me that l 
is the wrong type for splitter rather than printing out template 
constraints and such. When I see a bunch of lines of irrelevant 
error code it just makes me close my eyes.


This was a programming bug on my part but it goes to the root 
that error messages could be better.

Could they not simply print out the id of the variable and the 
type that is wrong rather than making it seem like the template 
is wrong(as if I'm not using it properly) or both.

If it would have said something like "type l is of char while 
splitter expects string" I would have realized the bug 
immediately. I didn't go down that path because I thought l was a 
string.

I was reading a text: readText("file") and for some reason I 
thought it would read it by lines or I was going to split it and 
forgot... so I was getting one large string rather than array of 
strings.

This then cascaded the problem and because of another issue of 
not using D for a while it confounded. The error that D displayed 
was not registering and too much useless info(for the actual 
error).

It would be very nice if D could have more user friendly error 
messages that try to pinpoint the bug rather than just throwing 
out generic information[which may or may not be all that helpful].

D should try to have some heuristic that figures out the actual 
real error and give the relevant info. If it mentioned l or s as 
being of the wrong type(which it could figure out where s comes 
from through l and the foreach) then I think I would have saved 
about 15 minutes and not have written this post.


This is litterally the error(When I change s back to a string)

1>Test.d(31): error : template `std.regex.split` cannot deduce 
function from argument types `!()(immutable(char), 
CTRegexWrapper!char)`, candidates are:
1>C:\Data\ldc2-1.17.0-windows-multilib\bin\..\import\std\regex\package.d(1603):        `std.regex.split(String, RegEx)(String input, RegEx rx) if (isSomeString!String && isRegexFor!(RegEx, String))`
1>Test.d(61): error : template instance `Test.main.rate!(d, "")` 
error instantiating
1>Test.d(66): error : cannot implicitly convert expression `l` of 
type `immutable(char)` to `string`
1>Test.d(31): error : template `std.regex.split` cannot deduce 
function from argument types `!()(immutable(char), 
CTRegexWrapper!char)`, candidates are:
1>C:\Data\ldc2-1.17.0-windows-multilib\bin\..\import\std\regex\package.d(1603):        `std.regex.split(String, RegEx)(String input, RegEx rx) if (isSomeString!String && isRegexFor!(RegEx, String))`
1>Test.d(79): error : template instance `Test.main.rate!(res, " 
")` error instantiating


This is very annoying. It talks about regex, CTRegexWrapper...

so much BS for such a simple error. To me these error messages 
are counter productive. It's just a wall of crap that I have to 
sift through and analyze and then reason to the bug.

1>Test.d(66): error : cannot implicitly convert expression `l` of 
type `immutable(char)` to `string`

I realize the error is right there but notice how it is buried 
within several lines of BS.

See all the regex references? That is why I thought it was a 
problem with regex and it led me on a wild goose chase. I figured 
that it had to be a regex problem(again, because I was assuming l 
was a string). I realize it was my faulty logic, but it was 
greatly exacerbated by all the junk D prints out thinking it 
helps(maybe it does sometimes but sometimes it doesn't).










More information about the Digitalmars-d-learn mailing list