www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cannot infer argument types

reply "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
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!
Nov 12 2013
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, November 13, 2013 03:43:40 bioinfornatics wrote:
 Hi,
=20
 I have this error message ( i.e title ) and i do not see where i
 am wrong:
=20
=20
      this( in ubyte wordLength, in string sequence ){
          kMer =3D wordLength;
          bytePerChar =3D cast(ubyte)(T.sizeof / kMer);
=20
          char[ubyte] toCharTmp;
          ubyte[char] toNumTmp;
          foreach( ubyte i, const char letter;
 sequence.dup.sort.uniq() ){
              if(i !in toCharTmp)
                  toCharTmp[ i ]  =3D letter;
              toNumTmp[ letter ]  =3D i;
          }
          toChar  =3D toCharTmp.rehash;
          toNum   =3D toNumTmp.rehash;
      }
=20
 Error is given at <foreach line>
=20
 I have removed explicit type on this line=E2=80=A6 but I have always =
same
 error!
You have two variables to the left to the ; in the foreach. If you were= =20 iterating over an array, then the first one would be the index, and the= second=20 one would be the element. However, the result of sequence.dup.sort.uniq= () is a=20 range that is _not_ an array, and foreach does not support an index for= =20 ranges. You can fake it with lockstep if you want, since it does someth= ing=20 with tuples to make it so that you get multiple elements on the left-ha= nd side=20 of the semicolon. http://dlang.org/phobos/std_range.html#lockstep In addition, I would point out that sequence.dup.sort is using the buil= t-in=20 sort for arrays rather than std.algorithm.sort (you have to have the pa= rens=20 when calling sort on array, or it'll use the built-in one), and the bui= lt-in=20 sort for arrays is not only buggy, but it's going to be deprecated, so = I=20 wouldn't advise using it. And yes, the means that you'll have to have a= =20 random-access range, meaning that you'll need to convert your string to= =20 dchar[], but at least then you'll get a sort that works and isn't going= to be=20 removed from the language (IIRC, the built-in sort doesn't sort Unicode= =20 properly anyway). If you know that you only have ASCII characters, then= you=20 can use ubyte[] instead, but char[] isn't going to work, since it's not= a=20 random-access range. - Jonathan M Davis
Nov 12 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 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.
I'd like dmd to give a deprecation warning when you use a built-in sort. And regarding sorting ASCII chars, it's a common need. I usually do it this way (if the input array of chars is mutable the code could spare the dup): string s = "test"; string t = cast(string)(s.dup.representation.sort().release); See also: https://d.puremagic.com/issues/show_bug.cgi?id=10162 Bye, bearophile
Nov 13 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, November 13, 2013 11:02:15 bearophile wrote:
 I'd like dmd to give a deprecation warning when you use a
 built-in sort.
Agreed, but it's not officially deprecated yet, much as I think that it's supposed to be.
 And regarding sorting ASCII chars, it's a common need. I usually
 do it this way (if the input array of chars is mutable the code
 could spare the dup):
Sure, but you can't sort it as char[] that way, and you have to be sure that you're dealing with ASCII-only.
 string s = "test";
 string t = cast(string)(s.dup.representation.sort().release);
I keep forgetting about std.string.representation. I always end up casting (though representation is arguably better). - Jonathan M Davis
Nov 13 2013
prev sibling parent "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
On Wednesday, 13 November 2013 at 02:43:42 UTC, 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!
Thanks a lot Davis and bearophile that is really interesting I though that I used std.algorithm.sort not a builtin… .
Nov 13 2013