foreach over split string

JS js.mdnq at gmail.com
Wed Jul 17 07:38:41 PDT 2013


On Wednesday, 17 July 2013 at 14:18:25 UTC, John Colvin wrote:
> On Wednesday, 17 July 2013 at 14:09:28 UTC, JS wrote:
>> Ok, spoke too soon again,
>>
>> my string requires compound splitting:
>>
>> foreach(ss; split(s, ","))
>> {
>>    split(ss, "|"); // ss can't be read at compile time 
>> although I can use ss directly
>>    string a = ss; // works fine.
>> }
>
> Is there any possibility you could provide a compilable example 
> (or not compilable, if that's the point :p )? It's an awful lot 
> easier to quickly give the right answer to a question if you 
> take the time to ask it clearly and precisely, in one go, with 
> a minimised example.
> Also, by virtue of you having made an example and checked it, 
> it prevents accidentally talking about problems in code that is 
> actually fine.

Yes, but if ctfe's wern't so shitty it wouldn't be a problem in 
the first place. The error messages are crap, the type system is 
disjoint(passing strings to templates must be distinguished in 
many cases from types).

All I did was create a wrapper to std.string.split. when I use 
it, the code breaks... I don't get any obvious error about my 
template but just that I can't use a non-compile time variable.

What you don't understand it is is a lot of work to simplify my 
code to something that will make sense so you can "give your 
quick answer"(which is not what I'm looking for. This isn't stack 
overflow and you don't get a golden ticket for answering 
correctly).

There is also a lot of support code that I use which I have to 
remove... it's a lot more work than you want to make it out to 
be. What I'm learning is that error messages in CTFE's are about 
useless... but they still throw me for a loop.


If it will make you feel better to see some code, which does work,


template tSplitStr(string n, string d = " ") { enum tSplitStr = 
std.string.split(n, d); }


/// Implements each (name, type) pair in T
template tPImplS(T...)
{
	string eval()
	{
		string s;
		foreach(nt; tSplitStr!(tPDeclH!(T), "&"))
		{
			string name = (std.string.split(nt, ","))[0];
			string tname = (std.string.split(nt, ","))[1];
			s ~= "private "~tname~" _"~name~";\n";
			s ~= "@property "~tname~" "~name~"() { return 
_"~name~"}\n"~"@property "~tname~" "~name~"("~tname~" value) { 
return _"~name~" = value; }\n";
		}

		return s;
	}
	enum tPImplS = eval();
}

but note when I try to use the tSplitStr, I get errors. This 
makes no sense to me because tSplitStr simply wraps split, and I 
should be able to use it where I use split. I do realize one is a 
template and the other is ctfe... and realize that tSplitStr does 
work, in some cases, just not all(which is what makes it a bitch 
to figure out with errors like "nt can't be read at compile 
time").

My guess is the compiler is not smart enough to realize that 
tSplitStr does use any compile time features and really is just a 
wrapper to split.


It seems that if the compiler see's ! used it throws the generic 
error "cannot be read at compile time"... which I'm so sick of 
seeing it's driving me insane.





More information about the Digitalmars-d-learn mailing list