www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - importing std.algorithm breaks std.string.count

reply Seth Hoenig <seth.a.hoenig gmail.com> writes:
--0016e68ddd871211e104907b10c7
Content-Type: text/plain; charset=ISO-8859-1

I have these two minimal programs:


import std.string;
void main()
{
    string str = "abc";
    int i = str.count("ab");

}



and:



import std.string;
import std.algorithm;
void main()
{
    string str = "abc";
    int i = str.count("ab");

}



The only difference is line 2, where I import std.algorithm.
The first program compiles fine, but the second program does not compile,
spitting out the error message:

bash-3.2$ dmd -ofdummy dummy.d
/u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(176): Error:
static assert  "Bad binary function q{a == b}. You need to use a valid D
expression using symbols a of type dchar and b of type string."
/u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(179):
instantiated from here: Body!(dchar,string)
/u/sah2659/dmd2/linux/bin/../../src/phobos/std/algorithm.d(3410):
instantiated from here: result!(dchar,string)
dummy.d(7):        instantiated from here: count!("a == b",string,string)


I can't imagine I'm the first person to notice a bug like this, so is there
something I am doing wrong?

--0016e68ddd871211e104907b10c7
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I have these two minimal programs:<br><br><br>import std.string;<br>void ma=
in()<br>{<br>=A0=A0=A0 string str =3D &quot;abc&quot;;<br>=A0=A0=A0 int i =
=3D str.count(&quot;ab&quot;);<br>=A0=A0=A0 <br>}<br><br><br><br>and:<br><b=
r><br><br>import std.string;<br>
import std.algorithm;<br>void main()<br>{<br>=A0=A0=A0 string str =3D &quot=
;abc&quot;;<br>=A0=A0=A0 int i =3D str.count(&quot;ab&quot;);<br>=A0=A0=A0 =
<br>}<br><br><br><br>The only difference is line 2, where I import std.algo=
rithm. <br>The first program compiles fine, but the second program does not=
 compile, spitting out the error message:<br>
<br>bash-3.2$ dmd -ofdummy dummy.d<br>/u/sah2659/dmd2/linux/bin/../../src/p=
hobos/std/functional.d(176): Error: static assert=A0 &quot;Bad binary funct=
ion q{a =3D=3D b}. You need to use a valid D expression using symbols a of =
type dchar and b of type string.&quot;<br>
/u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(179):=A0=A0=A0=
=A0=A0=A0=A0 instantiated from here: Body!(dchar,string)<br>/u/sah2659/dmd2=
/linux/bin/../../src/phobos/std/algorithm.d(3410):=A0=A0=A0=A0=A0=A0=A0 ins=
tantiated from here: result!(dchar,string)<br>
dummy.d(7):=A0=A0=A0=A0=A0=A0=A0 instantiated from here: count!(&quot;a =3D=
=3D b&quot;,string,string)<br><br><br>I can&#39;t imagine I&#39;m the first=
 person to notice a bug like this, so is there something I am doing wrong?<=
br>

--0016e68ddd871211e104907b10c7--
Sep 17 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 17 Sep 2010 17:15:31 -0400, Seth Hoenig <seth.a.hoenig gmail.com>  
wrote:

 I have these two minimal programs:


 import std.string;
 void main()
 {
     string str = "abc";
     int i = str.count("ab");

 }



 and:



 import std.string;
 import std.algorithm;
 void main()
 {
     string str = "abc";
     int i = str.count("ab");

 }



 The only difference is line 2, where I import std.algorithm.
 The first program compiles fine, but the second program does not compile,
 spitting out the error message:

 bash-3.2$ dmd -ofdummy dummy.d
 /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(176): Error:
 static assert  "Bad binary function q{a == b}. You need to use a valid D
 expression using symbols a of type dchar and b of type string."
 /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(179):
 instantiated from here: Body!(dchar,string)
 /u/sah2659/dmd2/linux/bin/../../src/phobos/std/algorithm.d(3410):
 instantiated from here: result!(dchar,string)
 dummy.d(7):        instantiated from here: count!("a == b",string,string)


 I can't imagine I'm the first person to notice a bug like this, so is  
 there
 something I am doing wrong?

I see two bugs here. First, this should be an ambiguity error, because count matches both std.algorithm.count and std.string.count. The compiler should refuse to compile this I think. Second, std.algorithm.count looks like this: size_t count(alias pred = "a == b", Range, E)(Range r, E value) if (isInputRange!(Range)) So, E can be any type, completely unrelated to strings, I could do: count!(string, int[]) which makes no sense. I think the sig should be size_t count(alias pred = "a == b", Range, E)(Range r, E value) if (isInputRange!(Range) && isImplicitlyConvertable!(E, ElementType!(Range))) -Steve
Sep 17 2010
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Steven Schveighoffer" <schveiguy yahoo.com> wrote in message 
news:op.vi64pdhveav7ka localhost.localdomain...
 Second, std.algorithm.count looks like this:

 size_t count(alias pred = "a == b", Range, E)(Range r, E value) if 
 (isInputRange!(Range))

 So, E can be any type, completely unrelated to strings, I could do:

 count!(string, int[]) which makes no sense.

 I think the sig should be

 size_t count(alias pred = "a == b", Range, E)(Range r, E value) if 
 (isInputRange!(Range) && isImplicitlyConvertable!(E, ElementType!(Range)))

 -Steve

Except you can call it like this: string s = "1234"; int[] a = [1, 2, 3]; count!"cast(int)(a - '0') == b.length"(s, a); Which is perfectly valid.
Sep 22 2010
prev sibling next sibling parent Seth Hoenig <seth.a.hoenig gmail.com> writes:
--0016367658308aa95a04907bb4d0
Content-Type: text/plain; charset=ISO-8859-1

Submitted *Issue 4883 <http://d.puremagic.com/issues/show_bug.cgi?id=4883>


*
On Fri, Sep 17, 2010 at 4:30 PM, Steven Schveighoffer
<schveiguy yahoo.com>wrote:

 On Fri, 17 Sep 2010 17:15:31 -0400, Seth Hoenig <seth.a.hoenig gmail.com>
 wrote:

  I have these two minimal programs:
 import std.string;
 void main()
 {
    string str = "abc";
    int i = str.count("ab");

 }



 and:



 import std.string;
 import std.algorithm;
 void main()
 {
    string str = "abc";
    int i = str.count("ab");

 }



 The only difference is line 2, where I import std.algorithm.
 The first program compiles fine, but the second program does not compile,
 spitting out the error message:

 bash-3.2$ dmd -ofdummy dummy.d
 /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(176): Error:
 static assert  "Bad binary function q{a == b}. You need to use a valid D
 expression using symbols a of type dchar and b of type string."
 /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(179):
 instantiated from here: Body!(dchar,string)
 /u/sah2659/dmd2/linux/bin/../../src/phobos/std/algorithm.d(3410):
 instantiated from here: result!(dchar,string)
 dummy.d(7):        instantiated from here: count!("a == b",string,string)


 I can't imagine I'm the first person to notice a bug like this, so is
 there
 something I am doing wrong?

I see two bugs here. First, this should be an ambiguity error, because count matches both std.algorithm.count and std.string.count. The compiler should refuse to compile this I think. Second, std.algorithm.count looks like this: size_t count(alias pred = "a == b", Range, E)(Range r, E value) if (isInputRange!(Range)) So, E can be any type, completely unrelated to strings, I could do: count!(string, int[]) which makes no sense. I think the sig should be size_t count(alias pred = "a == b", Range, E)(Range r, E value) if (isInputRange!(Range) && isImplicitlyConvertable!(E, ElementType!(Range))) -Steve

--0016367658308aa95a04907bb4d0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Submitted <b><a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D488= 3"><b>Issue=A04883</b></a><br><br><br></b><br><div class=3D"gmail_quote">On= Fri, Sep 17, 2010 at 4:30 PM, Steven Schveighoffer <span dir=3D"ltr">&lt;<= a href=3D"mailto:schveiguy yahoo.com">schveiguy yahoo.com</a>&gt;</span> wr= ote:<br> <blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, = 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><= div class=3D"h5">On Fri, 17 Sep 2010 17:15:31 -0400, Seth Hoenig &lt;<a hre= f=3D"mailto:seth.a.hoenig gmail.com" target=3D"_blank">seth.a.hoenig gmail.= com</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, = 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> I have these two minimal programs:<br> <br> <br> import std.string;<br> void main()<br> {<br> =A0 =A0string str =3D &quot;abc&quot;;<br> =A0 =A0int i =3D str.count(&quot;ab&quot;);<br> <br> }<br> <br> <br> <br> and:<br> <br> <br> <br> import std.string;<br> import std.algorithm;<br> void main()<br> {<br> =A0 =A0string str =3D &quot;abc&quot;;<br> =A0 =A0int i =3D str.count(&quot;ab&quot;);<br> <br> }<br> <br> <br> <br> The only difference is line 2, where I import std.algorithm.<br> The first program compiles fine, but the second program does not compile,<b= r> spitting out the error message:<br> <br> bash-3.2$ dmd -ofdummy dummy.d<br> /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(176): Error:<br=

valid D<br> expression using symbols a of type dchar and b of type string.&quot;<br> /u/sah2659/dmd2/linux/bin/../../src/phobos/std/functional.d(179):<br> instantiated from here: Body!(dchar,string)<br> /u/sah2659/dmd2/linux/bin/../../src/phobos/std/algorithm.d(3410):<br> instantiated from here: result!(dchar,string)<br> dummy.d(7): =A0 =A0 =A0 =A0instantiated from here: count!(&quot;a =3D=3D b&= quot;,string,string)<br> <br> <br> I can&#39;t imagine I&#39;m the first person to notice a bug like this, so = is there<br> something I am doing wrong?<br> </blockquote> <br></div></div> I see two bugs here. =A0First, this should be an ambiguity error, because c= ount matches both std.algorithm.count and std.string.count. =A0The compiler= should refuse to compile this I think.<br> <br> Second, std.algorithm.count looks like this:<br> <br> size_t count(alias pred =3D &quot;a =3D=3D b&quot;, Range, E)(Range r, E va= lue) if (isInputRange!(Range))<br> <br> So, E can be any type, completely unrelated to strings, I could do:<br> <br> count!(string, int[]) which makes no sense.<br> <br> I think the sig should be<br> <br> size_t count(alias pred =3D &quot;a =3D=3D b&quot;, Range, E)(Range r, E va= lue) if (isInputRange!(Range) &amp;&amp; isImplicitlyConvertable!(E, Elemen= tType!(Range)))<br> <br> -Steve<br> </blockquote></div><br> --0016367658308aa95a04907bb4d0--
Sep 17 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 23 Sep 2010 02:19:55 -0400, Daniel Murphy  
<yebblies nospamgmail.com> wrote:

 "Steven Schveighoffer" <schveiguy yahoo.com> wrote in message
 news:op.vi64pdhveav7ka localhost.localdomain...
 Second, std.algorithm.count looks like this:

 size_t count(alias pred = "a == b", Range, E)(Range r, E value) if
 (isInputRange!(Range))

 So, E can be any type, completely unrelated to strings, I could do:

 count!(string, int[]) which makes no sense.

 I think the sig should be

 size_t count(alias pred = "a == b", Range, E)(Range r, E value) if
 (isInputRange!(Range) && isImplicitlyConvertable!(E,  
 ElementType!(Range)))

 -Steve

Except you can call it like this: string s = "1234"; int[] a = [1, 2, 3]; count!"cast(int)(a - '0') == b.length"(s, a); Which is perfectly valid.

and insane :) count(s, cast(char)(a.length + '0')); -Steve
Sep 23 2010