www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is std.array.replace supposed to work with char[]?

reply Jacob Carlborg <doob me.com> writes:
Compiling the following code with DMD 2.052 on Mac OS X:

import std.array;

void main ()
{
     char[] a;
     char[] b;
     a.replace(1, 3, b);
}

Results in the following error:

test.d(7): Error: template std.array.replace(T,Range) if 
(isDynamicArray!(Range) && is(ElementType!(Range) : T)) does not match 
any function template declaration
test.d(7): Error: template std.array.replace(T,Range) if 
(isDynamicArray!(Range) && is(ElementType!(Range) : T)) cannot deduce 
template function from argument types !()(char[],int,int,char[])

What am I doing wrong or isn't std.array.replace supposed to work with 
char[]?

-- 
/Jacob Carlborg
Feb 19 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 19 Feb 2011 16:23:12 -0500, Jacob Carlborg <doob me.com> wrote:

 Compiling the following code with DMD 2.052 on Mac OS X:

 import std.array;

 void main ()
 {
      char[] a;
      char[] b;
      a.replace(1, 3, b);
 }

 Results in the following error:

 test.d(7): Error: template std.array.replace(T,Range) if  
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) does not match  
 any function template declaration
 test.d(7): Error: template std.array.replace(T,Range) if  
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) cannot deduce  
 template function from argument types !()(char[],int,int,char[])

 What am I doing wrong or isn't std.array.replace supposed to work with  
 char[]?
D currently suffers from a form of schizophrenia. in Phobos, char[] and wchar[] (and related const/immutable versions) are *NOT* treated as arrays of char and wchar. They are treated as bi-directional ranges of dchar. The compiler does not see it this way, it thinks they are arrays. The error in your assumption is that Range's element type is char, when in fact it is dchar (Range is the type of the second char[]). So the is(ElementType!(char[]) : char) fails. -Steve
Feb 19 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-02-19 23:20, Steven Schveighoffer wrote:
 On Sat, 19 Feb 2011 16:23:12 -0500, Jacob Carlborg <doob me.com> wrote:

 Compiling the following code with DMD 2.052 on Mac OS X:

 import std.array;

 void main ()
 {
 char[] a;
 char[] b;
 a.replace(1, 3, b);
 }

 Results in the following error:

 test.d(7): Error: template std.array.replace(T,Range) if
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) does not match
 any function template declaration
 test.d(7): Error: template std.array.replace(T,Range) if
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) cannot deduce
 template function from argument types !()(char[],int,int,char[])

 What am I doing wrong or isn't std.array.replace supposed to work with
 char[]?
D currently suffers from a form of schizophrenia. in Phobos, char[] and wchar[] (and related const/immutable versions) are *NOT* treated as arrays of char and wchar. They are treated as bi-directional ranges of dchar. The compiler does not see it this way, it thinks they are arrays. The error in your assumption is that Range's element type is char, when in fact it is dchar (Range is the type of the second char[]). So the is(ElementType!(char[]) : char) fails. -Steve
So you're saying that I can only use dstrings for anything with the same template constraint? -- /Jacob Carlborg
Feb 20 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 20 Feb 2011 09:45:36 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-02-19 23:20, Steven Schveighoffer wrote:
 On Sat, 19 Feb 2011 16:23:12 -0500, Jacob Carlborg <doob me.com> wrote:

 Compiling the following code with DMD 2.052 on Mac OS X:

 import std.array;

 void main ()
 {
 char[] a;
 char[] b;
 a.replace(1, 3, b);
 }

 Results in the following error:

 test.d(7): Error: template std.array.replace(T,Range) if
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) does not match
 any function template declaration
 test.d(7): Error: template std.array.replace(T,Range) if
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) cannot deduce
 template function from argument types !()(char[],int,int,char[])

 What am I doing wrong or isn't std.array.replace supposed to work with
 char[]?
D currently suffers from a form of schizophrenia. in Phobos, char[] and wchar[] (and related const/immutable versions) are *NOT* treated as arrays of char and wchar. They are treated as bi-directional ranges of dchar. The compiler does not see it this way, it thinks they are arrays. The error in your assumption is that Range's element type is char, when in fact it is dchar (Range is the type of the second char[]). So the is(ElementType!(char[]) : char) fails. -Steve
So you're saying that I can only use dstrings for anything with the same template constraint?
Yes, dstrings act like arrays according to phobos. wstrings and strings do not. Some functions have specialized versions for strings, some do not. Expect to be confused... -Steve
Feb 20 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-02-20 17:12, Steven Schveighoffer wrote:
 On Sun, 20 Feb 2011 09:45:36 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-02-19 23:20, Steven Schveighoffer wrote:
 On Sat, 19 Feb 2011 16:23:12 -0500, Jacob Carlborg <doob me.com> wrote:

 Compiling the following code with DMD 2.052 on Mac OS X:

 import std.array;

 void main ()
 {
 char[] a;
 char[] b;
 a.replace(1, 3, b);
 }

 Results in the following error:

 test.d(7): Error: template std.array.replace(T,Range) if
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) does not match
 any function template declaration
 test.d(7): Error: template std.array.replace(T,Range) if
 (isDynamicArray!(Range) && is(ElementType!(Range) : T)) cannot deduce
 template function from argument types !()(char[],int,int,char[])

 What am I doing wrong or isn't std.array.replace supposed to work with
 char[]?
D currently suffers from a form of schizophrenia. in Phobos, char[] and wchar[] (and related const/immutable versions) are *NOT* treated as arrays of char and wchar. They are treated as bi-directional ranges of dchar. The compiler does not see it this way, it thinks they are arrays. The error in your assumption is that Range's element type is char, when in fact it is dchar (Range is the type of the second char[]). So the is(ElementType!(char[]) : char) fails. -Steve
So you're saying that I can only use dstrings for anything with the same template constraint?
Yes, dstrings act like arrays according to phobos. wstrings and strings do not. Some functions have specialized versions for strings, some do not. Expect to be confused... -Steve
I'm confused about how someone can implement a library like this. Every time I try to use D2 it's just a PITA to use. I've used D1 and Tango for several years and had no problem with that. I assume this has been discussed, did that resolve in any plans to solve this? -- /Jacob Carlborg
Feb 20 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Jacob Carlborg:

 Every time I try to use D2 it's just a PITA to use. I've used D1 and Tango for 
 several years and had no problem with that.
I use this thread to ask regarding one specific little problem I have with strings. I want to generate a random string of AB using the array, map, etc, this looks like a possible implementation (in std.random there is no choice() function yet): import std.stdio, std.random, std.string, std.algorithm, std.range; void main() { auto m = map!((i){ return "AB"[uniform(0,2)]; })(iota(10)); string s = join(array(m)); writeln(s); } It gives this error: ...\dmd\src\phobos\std\array.d(62): Error: result[i] isn't mutable test.d(5): Error: template instance std.array.array!(Map!(__dgliteral1,Iota!(int,uint))) error instantiating What's the right way to write it in D? The same code in Python2.x: from random import choice s = "".join(choice("AB") for _ in xrange(10)) print s Bye, bearophile
Feb 20 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 20 Feb 2011 14:51:10 -0500, bearophile <bearophileHUGS lycos.com>  
wrote:

 Jacob Carlborg:

 Every time I try to use D2 it's just a PITA to use. I've used D1 and  
 Tango for
 several years and had no problem with that.
I use this thread to ask regarding one specific little problem I have with strings. I want to generate a random string of AB using the array, map, etc, this looks like a possible implementation (in std.random there is no choice() function yet): import std.stdio, std.random, std.string, std.algorithm, std.range; void main() { auto m = map!((i){ return "AB"[uniform(0,2)]; })(iota(10)); string s = join(array(m)); writeln(s); } It gives this error: ...\dmd\src\phobos\std\array.d(62): Error: result[i] isn't mutable test.d(5): Error: template instance std.array.array!(Map!(__dgliteral1,Iota!(int,uint))) error instantiating What's the right way to write it in D? The same code in Python2.x: from random import choice s = "".join(choice("AB") for _ in xrange(10)) print s
Just a blind guess, I have not tested, but maybe it's because the compiler is using const(char) as the return type for your delegate literal since you never specify one? -Steve
Feb 20 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Steven Schveighoffer:

 Just a blind guess, I have not tested, but maybe it's because the compiler  
 is using const(char) as the return type for your delegate literal since  
 you never specify one?
Right, this works: import std.stdio, std.random, std.string, std.algorithm, std.range; void main() { string s = array(map!((i){ return cast(char)("AB"[uniform(0,2)]); })(iota(10))).idup; writeln(s); } Thank you, bye, bearophile
Feb 20 2011
parent bearophile <bearophileHUGS lycos.com> writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5630
Feb 20 2011
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Sun, 20 Feb 2011 15:23:29 -0500, Steven Schveighoffer wrote:

 On Sun, 20 Feb 2011 14:51:10 -0500, bearophile
 <bearophileHUGS lycos.com> wrote:
 
 Jacob Carlborg:

 Every time I try to use D2 it's just a PITA to use. I've used D1 and
 Tango for
 several years and had no problem with that.
I use this thread to ask regarding one specific little problem I have with strings. I want to generate a random string of AB using the array, map, etc, this looks like a possible implementation (in std.random there is no choice() function yet): import std.stdio, std.random, std.string, std.algorithm, std.range; void main() { auto m = map!((i){ return "AB"[uniform(0,2)]; })(iota(10)); string s = join(array(m)); writeln(s); } It gives this error: ...\dmd\src\phobos\std\array.d(62): Error: result[i] isn't mutable test.d(5): Error: template instance std.array.array!(Map!(__dgliteral1,Iota!(int,uint))) error instantiating What's the right way to write it in D? The same code in Python2.x: from random import choice s = "".join(choice("AB") for _ in xrange(10)) print s
Just a blind guess, I have not tested, but maybe it's because the compiler is using const(char) as the return type for your delegate literal since you never specify one?
It's probably using immutable(char) since that's the element type of "AB". -Lars
Feb 21 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 20 Feb 2011 13:40:08 -0500, Jacob Carlborg <doob me.com> wrote:


 I'm confused about how someone can implement a library like this. Every  
 time I try to use D2 it's just a PITA to use. I've used D1 and Tango for  
 several years and had no problem with that.
Strings are a sore spot for me, I think the current implementation is a hack which leaves much to be desired. However, it seems that Andrei believes the current implementation is not only decent, but actually better than any alternative.
 I assume this has been discussed, did that resolve in any plans to solve  
 this?
I am, in my spare time, working on a way to represent strings that allows strings to be what they are and arrays of chars or wchars to be what they are. It is not finished yet, but I've put forth a couple incomplete examples. Search for [review] on the D news group. Things have gotten quite more complex when I realized that dchar is actually not the most natural "element" of a string, since a dchar doesn't represent a visible character (or one that a human would interpret as a single character) and also that the exact same string can be sometimes represented by two distinct sequences of dchars. When I can finish the proposed change, I will probably try integrating it with Phobos to see how well it works. -Steve
Feb 20 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-02-20 21:30, Steven Schveighoffer wrote:
 On Sun, 20 Feb 2011 13:40:08 -0500, Jacob Carlborg <doob me.com> wrote:


 I'm confused about how someone can implement a library like this.
 Every time I try to use D2 it's just a PITA to use. I've used D1 and
 Tango for several years and had no problem with that.
Strings are a sore spot for me, I think the current implementation is a hack which leaves much to be desired. However, it seems that Andrei believes the current implementation is not only decent, but actually better than any alternative.
 I assume this has been discussed, did that resolve in any plans to
 solve this?
I am, in my spare time, working on a way to represent strings that allows strings to be what they are and arrays of chars or wchars to be what they are. It is not finished yet, but I've put forth a couple incomplete examples. Search for [review] on the D news group. Things have gotten quite more complex when I realized that dchar is actually not the most natural "element" of a string, since a dchar doesn't represent a visible character (or one that a human would interpret as a single character) and also that the exact same string can be sometimes represented by two distinct sequences of dchars. When I can finish the proposed change, I will probably try integrating it with Phobos to see how well it works. -Steve
Oh, I remember that thread(s). It got so overly complicated so I stopped reading it. -- /Jacob Carlborg
Feb 20 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 20 Feb 2011 17:10:28 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-02-20 21:30, Steven Schveighoffer wrote:
 On Sun, 20 Feb 2011 13:40:08 -0500, Jacob Carlborg <doob me.com> wrote:


 I'm confused about how someone can implement a library like this.
 Every time I try to use D2 it's just a PITA to use. I've used D1 and
 Tango for several years and had no problem with that.
Strings are a sore spot for me, I think the current implementation is a hack which leaves much to be desired. However, it seems that Andrei believes the current implementation is not only decent, but actually better than any alternative.
 I assume this has been discussed, did that resolve in any plans to
 solve this?
I am, in my spare time, working on a way to represent strings that allows strings to be what they are and arrays of chars or wchars to be what they are. It is not finished yet, but I've put forth a couple incomplete examples. Search for [review] on the D news group. Things have gotten quite more complex when I realized that dchar is actually not the most natural "element" of a string, since a dchar doesn't represent a visible character (or one that a human would interpret as a single character) and also that the exact same string can be sometimes represented by two distinct sequences of dchars. When I can finish the proposed change, I will probably try integrating it with Phobos to see how well it works. -Steve
Oh, I remember that thread(s). It got so overly complicated so I stopped reading it.
Yeah, UTF is so overly complicated, it's difficult to properly implement it. -Steve
Feb 20 2011