foreach over split string
JS
js.mdnq at gmail.com
Wed Jul 17 16:02:26 PDT 2013
On Wednesday, 17 July 2013 at 18:00:15 UTC, Jonathan M Davis
wrote:
> On Wednesday, July 17, 2013 16:02:00 JS wrote:
>> foreach(n; std.string.split(s, ","))
>> {
>> // n can't be read at compile time
>> }
>>
>> using in a ctfe.
>>
>> How to loop over a string array in a ctfe?
>
> I would point out that if you're just splitting a string to
> iterate over it,
> then you should probably use std.algorithm.splitter, as it
> avoids allocating a
> new array.
>
> But the code that you have here appears to work just fine in
> CTFE for me, so
> you probably have something else going on in code that you
> didn't show which
> is making it so that CTFE isn't working for you.
>
> - Jonathan M Davis
I will try that but the error message given hides the real issue.
I think the problem is, as demonstrated, simply wrapping a
standard function in a template breaks ctfe.
I believe the issue is more so about the semantic analysis of
ctfe more than anything...
I can't put together a working example right now(other things to
do) but the gist of the matter is:
template strsplit(string n) { enum strsplit = std.string.split(n,
","); }
... inside a ctfe ...
foreach(n; strsplit!(s)) // doesn't work
foreach(n; std.string.split(s, ",")) // works
To me, this is confusing as hell and "n can't be read at compile
time" took me a good few hours to realize it was simply using the
template. I'm guessing that it has something to do with the enum
"return type" of strsplit but I've tried other various things.
More information about the Digitalmars-d-learn
mailing list