T[] (array of generic type)

seany seany at uni-bonn.de
Mon Nov 18 12:45:52 PST 2013


On Monday, 18 November 2013 at 20:42:36 UTC, Jon wrote:
> 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).


thank you, precisely this is what i was looking for, any peculiar 
pitfalls to be aware of?


More information about the Digitalmars-d-learn mailing list