digitalmars.D.learn - T[] (array of generic type)
- seany (11/11) Nov 18 2013 perhaps I sohould have myself played around, but I would love to
- JR (5/8) Nov 18 2013 Ali's book has a good introduction to templates:
- seany (2/2) Nov 18 2013 I read that book, but dont find this constructtion, that is why
- Philippe Sigaud (8/10) Nov 18 2013 IIRC I talk a bit about function templates in my tutorial. JR gave the
- seany (4/18) Nov 18 2013 ~200 pages, please give me some time before properly thanking you
- Dejan Lekic (4/19) Nov 19 2013 Philippe, i wonder whether you plan on generating ePUB file out of those...
- Craig Dillabaugh (3/17) Nov 19 2013 From scratch? :0)
- Jon (19/21) Nov 18 2013 Seany, you are on the right track for the function declaration, I
- seany (3/24) Nov 18 2013 thank you, precisely this is what i was looking for, any peculiar
- Jon (8/38) Nov 18 2013 With this particular usage, it should work the way you expect.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/5) Nov 18 2013 I will make such an addition. Thanks.
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
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
I read that book, but dont find this constructtion, that is why the question.
Nov 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...
Nov 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:~200 pages, please give me some time before properly thanking you (i would like to first read it)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
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:templates-tutorial.pdfI 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-(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
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:From scratch? :0)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 19 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).
Nov 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:thank you, precisely this is what i was looking for, any peculiar pitfalls to be aware of?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
On Monday, 18 November 2013 at 20:45:54 UTC, seany wrote:On Monday, 18 November 2013 at 20:42:36 UTC, Jon wrote: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! -JonOn Monday, 18 November 2013 at 20:20:38 UTC, seany wrote:thank you, precisely this is what i was looking for, any peculiar pitfalls to be aware of?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
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