www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - T[] (array of generic type)

reply "seany" <seany uni-bonn.de> writes:
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?
Nov 18 2013
parent reply "JR" <zorael gmail.com> writes:
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
Nov 18 2013
parent reply "seany" <seany uni-bonn.de> writes:
I read that book, but dont find this constructtion, that is why 
the question.
Nov 18 2013
next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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...
Nov 18 2013
next sibling parent "seany" <seany uni-bonn.de> writes:
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)
Nov 18 2013
prev sibling next sibling parent Dejan Lekic <dejan.lekic gmail.com> writes:
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? :)
Nov 19 2013
prev sibling parent "Craig Dillabaugh" <cdillaba cg.scs.carleton.ca> writes:
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...
From scratch? :0)
Nov 19 2013
prev sibling next sibling parent reply "Jon" <no spam.com> writes:
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).
Nov 18 2013
parent reply "seany" <seany uni-bonn.de> writes:
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?
Nov 18 2013
parent "Jon" <no spam.com> writes:
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
Nov 18 2013
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 18 2013