T[] (array of generic type)

Jon no at spam.com
Mon Nov 18 12:42:34 PST 2013


On Monday, 18 November 2013 at 20:20:38 UTC, seany wrote:
> I read that book, but dont find this constructtion, that is why 
> the question.

Seany, you are on the right track for the function declaration, I 
think the following code does what you are looking for:

import std.stdio;

void main() {
	int[4] myArray;
	assign(myArray, 5);
	writeln(myArray);              //prints [5, 5, 5, 5]
}

void assign(T)(T[] arr, T val)
{
	for(int i = 0; i < arr.length; i++)
	{
		arr[i] = val;
	}
}

D is great because the template system infers what type you are 
passing without having to explicitly instantiate the template 
first, i.e. assign!(int)(myArray, 5).


More information about the Digitalmars-d-learn mailing list