www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Immutable immutable strings??

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
I'm not sure whether the following a compiler/language bug or a Phobos
bug, but it's definitely some kind of bug:

	auto s = "abc";
	immutable t = "def";

	writeln(typeid(s));	// immutable(char)[]
	writeln(typeid(t));	// immutable(immutable(char)[])
				// (what is this supposed to mean?!)

	char ch = 'c';
	bool b = canFind(s, ch);	// OK
	bool c = canFind(t, ch);	// Compile error??

The compile error is:

test.d:11: Error: template std.algorithm.canFind(alias pred = "a == b",Range,V)
if (is(typeof(find!(pred)(range,value)))) does not match any function template
declaration
test.d:11: Error: template std.algorithm.canFind(alias pred = "a == b",Range,V)
if (is(typeof(find!(pred)(range,value)))) cannot deduce template function from
argument types !()(immutable(char[]),char)

Can somebody explain what's going on here?


T

-- 
There are two ways to write error-free programs; only the third one works.
Feb 10 2012
parent reply "Marco Leise" <Marco.Leise gmx.de> writes:
Am 10.02.2012, 19:07 Uhr, schrieb H. S. Teoh <hsteoh quickfur.ath.cx>:

 I'm not sure whether the following a compiler/language bug or a Phobos
 bug, but it's definitely some kind of bug:

 	auto s = "abc";
 	immutable t = "def";

 	writeln(typeid(s));	// immutable(char)[]
 	writeln(typeid(t));	// immutable(immutable(char)[])
 				// (what is this supposed to mean?!)

That is supposed to mean that you can neither modify the values of the characters in the array nor the array itself: t[1] = 'a'; // immutable(char) t.length = 2; // immutable(...[])
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 07:10 PM, Marco Leise wrote:
 Am 10.02.2012, 19:07 Uhr, schrieb H. S. Teoh <hsteoh quickfur.ath.cx>:

 I'm not sure whether the following a compiler/language bug or a Phobos
 bug, but it's definitely some kind of bug:

 auto s = "abc";
 immutable t = "def";

 writeln(typeid(s)); // immutable(char)[]
 writeln(typeid(t)); // immutable(immutable(char)[])
 // (what is this supposed to mean?!)

That is supposed to mean that you can neither modify the values of the characters in the array nor the array itself:

immutable(char[]) does already say that.
 t[1] = 'a'; // immutable(char)
 t.length = 2; // immutable(...[])

It is a runtime bug, and has been discussed before, but I am not sure if it has been filed (probably yes). A similar bug has recently been fixed for the compiler's internal formatting exposed through error messages/.stringof: http://d.puremagic.com/issues/show_bug.cgi?id=6941
Feb 10 2012