Regarding fixed size arrays

Meta jared771 at gmail.com
Fri Mar 14 18:23:04 PDT 2014


On Saturday, 15 March 2014 at 01:14:02 UTC, Meta wrote:
> On Saturday, 15 March 2014 at 00:11:22 UTC, bearophile wrote:
>> Do you think it's useful/worth supporting code like that?
>
> Yes, I'm surprised that doesn't currently work. I hate how 
> static arrays throw away their length at the drop of a hat. In 
> the meantime, here's a workaround:
>
> T[N + M] concat(T, int N, int M)(T[N] arr1, T[M] arr2)
> {
> 	T[N + M] result = arr1 ~ arr2;
> 	
> 	return result;
> }
>
> void main()
> {
> 	int[3] arr1;
> 	int[2] arr2;
> 	int[5] arr3 = concat(arr1, arr2);
>
>         //Error: array length mismatch
>         int[6] arr4 = concat(arr1, arr2);
> }

Err, sorry, the lengths as template args aren't necessary. Your 
example works fine with this small change:

int[5] foo(int[2] a, int[3] b)
{
     int[5] result = a ~ b;
	
     return result;
}

Though I am happy about how powerful D's pattern matching in 
template arguments is.


More information about the Digitalmars-d-learn mailing list