Jump to page: 1 2
Thread overview
T[] (array of generic type)
Nov 18, 2013
seany
Nov 18, 2013
JR
Nov 18, 2013
seany
Nov 18, 2013
Philippe Sigaud
Nov 18, 2013
seany
Nov 19, 2013
Dejan Lekic
Nov 19, 2013
Craig Dillabaugh
Nov 18, 2013
Jon
Nov 18, 2013
seany
Nov 18, 2013
Jon
Nov 18, 2013
Ali Çehreli
November 18, 2013
perhaps I sohould have myself played around, but I would love to ask this :

I want to make a function, that takes ay array (whose elements can be int, string, struct, etc) and a variable of the same type, of which the array in an array.

Like function(int[] arr, int var)
or function(string[] arr, string var)

etc.

A natural choice is fuction(T)(T[] array, T var)

but i dont find much info on this type on construction, is there any material introducing me to this type of construction?
November 18, 2013
On Monday, 18 November 2013 at 19:47:47 UTC, seany wrote:
> A natural choice is fuction(T)(T[] array, T var)
>
> but i dont find much info on this type on construction, is there any material introducing me to this type of construction?

Ali's book has a good introduction to templates: http://ddili.org/ders/d.en/templates.html

There's also Philippe Sigaud's 150+ page tutorial: https://github.com/PhilippeSigaud/D-templates-tutorial
November 18, 2013
I read that book, but dont find this constructtion, that is why the question.

November 18, 2013
On Mon, Nov 18, 2013 at 9:20 PM, seany <seany@uni-bonn.de> wrote:
> I read that book, but dont find this constructtion, that is why the question.

IIRC I talk a bit about function templates in my tutorial. JR gave the link (thanks!), another, more direct way is to directly download the pdf:

https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.pdf

(click on `view raw` to download)
Try p. 28 and following.


And I really should take the time to write this thing again...
November 18, 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).
November 18, 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?
November 18, 2013
On Monday, 18 November 2013 at 20:32:25 UTC, Philippe Sigaud wrote:
> On Mon, Nov 18, 2013 at 9:20 PM, seany <seany@uni-bonn.de> wrote:
>> I read that book, but dont find this constructtion, that is why the
>> question.
>
> IIRC I talk a bit about function templates in my tutorial. JR gave the
> link (thanks!), another, more direct way is to directly download the
> pdf:
>
> https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.pdf
>
> (click on `view raw` to download)
> Try p. 28 and following.
>
>
> And I really should take the time to write this thing again...

~200 pages, please give me some time before properly thanking you (i would like to first read it)
November 18, 2013
On Monday, 18 November 2013 at 20:45:54 UTC, seany wrote:
> 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?

With this particular usage, it should work the way you expect. Things can get a little hairy when you are trying to do more complicated compile-time checking and things like that. Philippe's guide and the D Language Reference should get you through 99% of any issues you face, but sometimes it takes a lot of trial and error!

-Jon

November 18, 2013
On 11/18/2013 12:20 PM, seany wrote:
> I read that book, but dont find this constructtion, that is why the
> question.
>

I will make such an addition. Thanks.

Ali

November 19, 2013
On Mon, 18 Nov 2013 21:32:11 +0100, Philippe Sigaud wrote:

> On Mon, Nov 18, 2013 at 9:20 PM, seany <seany@uni-bonn.de> wrote:
>> I read that book, but dont find this constructtion, that is why the question.
> 
> IIRC I talk a bit about function templates in my tutorial. JR gave the link (thanks!), another, more direct way is to directly download the pdf:
> 
> https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-
templates-tutorial.pdf
> 
> (click on `view raw` to download)
> Try p. 28 and following.
> 
> 
> And I really should take the time to write this thing again...

Philippe, i wonder whether you plan on generating ePUB file out of those TeX files? :)
« First   ‹ Prev
1 2