cannot infer argument types

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 12 18:54:23 PST 2013


On Wednesday, November 13, 2013 03:43:40 bioinfornatics wrote:
> Hi,
> 
> I have this error message ( i.e title ) and i do not see where i
> am wrong:
> 
> 
>      this( in ubyte wordLength, in string sequence ){
>          kMer = wordLength;
>          bytePerChar = cast(ubyte)(T.sizeof / kMer);
> 
>          char[ubyte] toCharTmp;
>          ubyte[char] toNumTmp;
>          foreach( ubyte i, const char letter;
> sequence.dup.sort.uniq() ){
>              if(i !in toCharTmp)
>                  toCharTmp[ i ]  = letter;
>              toNumTmp[ letter ]  = i;
>          }
>          toChar  = toCharTmp.rehash;
>          toNum   = toNumTmp.rehash;
>      }
> 
> Error is given at <foreach line>
> 
> I have removed explicit type on this line… but I have always same
> error!

You have two variables to the left to the ; in the foreach. If you were 
iterating over an array, then the first one would be the index, and the second 
one would be the element. However, the result of sequence.dup.sort.uniq() is a 
range that is _not_ an array, and foreach does not support an index for 
ranges. You can fake it with lockstep if you want, since it does something 
with tuples to make it so that you get multiple elements on the left-hand side 
of the semicolon.

http://dlang.org/phobos/std_range.html#lockstep

In addition, I would point out that sequence.dup.sort is using the built-in 
sort for arrays rather than std.algorithm.sort (you have to have the parens 
when calling sort on array, or it'll use the built-in one), and the built-in 
sort for arrays is not only buggy, but it's going to be deprecated, so I 
wouldn't advise using it. And yes, the means that you'll have to have a 
random-access range, meaning that you'll need to convert your string to 
dchar[], but at least then you'll get a sort that works and isn't going to be 
removed from the language (IIRC, the built-in sort doesn't sort Unicode 
properly anyway). If you know that you only have ASCII characters, then you 
can use ubyte[] instead, but char[] isn't going to work, since it's not a 
random-access range.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list