www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Appending immutable char implicit cast to int, bug or feature?

reply "ixid" <nuaccount gmail.com> writes:
This is simple code to create all genetic combinations from two 
organisms.

string[] mixGenes(string a, string b) {
	string[] result;

	foreach(i;0..2)
		foreach(j;0..2)
			foreach(k;2..4)
				foreach(m;2..4)
					result ~= [a[i]] ~ [b[j]] ~ [a[k]] ~ [b[m]];

	return result;
}

This works, however when I remove the brackets:

result ~= a[i] ~ b[j] ~ a[k] ~ b[m];

I get the error:

Error: incompatible types for ((cast(int)a[cast(uint)i]) ~ 
(cast(int)b[cast(uint)j])): 'int' and 'int'

Why, when trying to append immutable chars to make a string, has 
it decided to implicitly cast them to int?
Dec 05 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/05/2012 09:30 AM, ixid wrote:
 This is simple code to create all genetic combinations from two organisms.

 string[] mixGenes(string a, string b) {
 string[] result;

 foreach(i;0..2)
 foreach(j;0..2)
 foreach(k;2..4)
 foreach(m;2..4)
 result ~= [a[i]] ~ [b[j]] ~ [a[k]] ~ [b[m]];

 return result;
 }

 This works, however when I remove the brackets:

 result ~= a[i] ~ b[j] ~ a[k] ~ b[m];

 I get the error:

 Error: incompatible types for ((cast(int)a[cast(uint)i]) ~
 (cast(int)b[cast(uint)j])): 'int' and 'int'

 Why, when trying to append immutable chars to make a string, has it
 decided to implicitly cast them to int?

I don't know where that cast occurs but I wanted to state the obvious: Operator ~ is defined only for arrays. Ali
Dec 05 2012
prev sibling parent "ixid" <nuaccount gmail.com> writes:
 I don't know where that cast occurs but I wanted to state the 
 obvious: Operator ~ is defined only for arrays.

Would having it also work for individual units to make an array be a plausible enhancement request? It would seem like a natural use of the operator.
Dec 06 2012