March 09, 2008 string and char[] conflict | ||||
---|---|---|---|---|
| ||||
I use a foreach to iterate thourgh the lines of a text file: foreach(ulong lnumber, char[] line; input) and I use std.string:strip() to take out unnecessary spaces: line=strip(line); Which gives out the following error: test.d(142): function std.string.strip (invariant(char)[]) does not match parameter types (char[]) test.d(142): Error: cannot implicitly convert expression (line) of type char[] to invariant(char)[] test.d(142): Error: cannot implicitly convert expression (strip(cast(invariant(char)[])line)) of type invariant(char)[] to char[] changing the line will correct the problem: line=strip(line.idup).dup; Is there a cleaner way to do this? I tried using foreach(ulong lnumber, string line; input) but it wont compile: test.d(136): function std.stream.InputStream.opApply (int delegate(ref char[] line)) does not match parameter types (int delegate(ref ulong __applyArg0, ref invariant(char)[] __applyArg1)) test.d(136): Error: cannot implicitly convert expression (__foreachbody15) of type int delegate(ref ulong __a pplyArg0, ref invariant(char)[] __applyArg1) to int delegate(ref ulong n, ref wchar[] line) As you might have assumed, im using lastest DMD2. |
March 09, 2008 Re: string and char[] conflict | ||||
---|---|---|---|---|
| ||||
Posted in reply to qadasd | qadasd wrote:
> I use a foreach to iterate thourgh the lines of a text file:
>
> foreach(ulong lnumber, char[] line; input)
>
> and I use std.string:strip() to take out unnecessary spaces:
>
> line=strip(line);
>
> Which gives out the following error:
> test.d(142): function std.string.strip (invariant(char)[]) does not
> match parameter types (char[])
> test.d(142): Error: cannot implicitly convert expression (line) of type
> char[] to invariant(char)[]
> test.d(142): Error: cannot implicitly convert expression
> (strip(cast(invariant(char)[])line)) of type invariant(char)[] to char[]
>
> changing the line will correct the problem:
>
> line=strip(line.idup).dup;
>
> Is there a cleaner way to do this?
>
> I tried using
>
> foreach(ulong lnumber, string line; input)
>
> but it wont compile:
>
> test.d(136): function std.stream.InputStream.opApply (int delegate(ref
> char[] line)) does not match parameter
> types (int delegate(ref ulong __applyArg0, ref invariant(char)[]
> __applyArg1))
> test.d(136): Error: cannot implicitly convert expression
> (__foreachbody15) of type int delegate(ref ulong __a
> pplyArg0, ref invariant(char)[] __applyArg1) to int delegate(ref ulong
> n, ref wchar[] line)
>
> As you might have assumed, im using lastest DMD2.
foreach(u,l;args)
strip(cast(string)l);
this works (if this is the only reference to the data) but it requiers an
ugly cast and
|
Copyright © 1999-2021 by the D Language Foundation