Can a D library have some types determined by the client program?

2024-03-07 Thread Liam McGillivray via Digitalmars-d-learn
In a source library written in D, is it possible to have some objects, variables, pointers etc which are determined by the program using the library? An example of where this would be useful is in the library I am currently writing. I have a class called `Map`, which holds an array of objects

Re: Can a D library have some types determined by the client program?

2024-03-07 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
There are two ways to do this. 1. Use templates. https://tour.dlang.org/tour/en/basics/templates 2. Use a factory function. https://tour.dlang.org/tour/en/basics/delegates ```d class Map(ATile : Tile) { ATile[] tiles; } ``` Or: ```d class Map { Tile[] tiles; Tile delega

Re: Can a D library have some types determined by the client program?

2024-03-07 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 7 March 2024 at 22:18:40 UTC, Richard (Rikki) Andrew Cattermole wrote: There are two ways to do this. 1. Use templates. https://tour.dlang.org/tour/en/basics/templates 2. Use a factory function. https://tour.dlang.org/tour/en/basics/delegates ```d class Map(ATile : Tile) {

Re: Can a D library have some types determined by the client program?

2024-03-07 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
On 08/03/2024 4:09 PM, Liam McGillivray wrote: On Thursday, 7 March 2024 at 22:18:40 UTC, Richard (Rikki) Andrew Cattermole wrote: There are two ways to do this. 1. Use templates. https://tour.dlang.org/tour/en/basics/templates 2. Use a factory function. https://tour.dlang.org/tour/en/basics

Re: Can a D library have some types determined by the client program?

2024-03-07 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 8 March 2024 at 03:19:59 UTC, Richard (Rikki) Andrew Cattermole wrote: On 08/03/2024 4:09 PM, Liam McGillivray wrote: Thank you. Is this first example you gave the template? Is the syntax `(ATile : Tile)` saying that ATile must be a derived class of Tile? If this isn't worse in an

Re: Can a D library have some types determined by the client program?

2024-03-08 Thread cc via Digitalmars-d-learn
On Friday, 8 March 2024 at 06:03:51 UTC, Liam McGillivray wrote: A problem I have is that the 3 classes Map, Tile, and Unit reference each-other. If I turn Map into a template, than it complains about the member variable of Unit declared as `Map map;` without arguments. I change this line to `M

Re: Can a D library have some types determined by the client program?

2024-03-08 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 8 March 2024 at 16:54:48 UTC, cc wrote: If you don't want Unit to be a template, you can just have Map derive from a basic interface or abstract class. You can also have every relevant class share similar templates, you just need to remember to supply the template arguments everywhe

Re: Can a D library have some types determined by the client program?

2024-03-09 Thread Liam McGillivray via Digitalmars-d-learn
Update on two things: One is that I now better understand what it means that D objects are "reference by default". This means that references *can* be null if they are declared with a class. In my commits last night, I have changed many pointers into references. I think my time will be smooth

Re: Can a D library have some types determined by the client program?

2024-03-09 Thread Liam McGillivray via Digitalmars-d-learn
I have made a new branch of my project called "templates-interfaces" which reworks some things, and turns the Map class into an interface and template. It is now functioning like the master branch, but I think the code should now be (arguably) easier to follow. At least that's true for the Rayl

Re: Can a D library have some types determined by the client program?

2024-03-10 Thread cc via Digitalmars-d-learn
On Saturday, 9 March 2024 at 22:03:34 UTC, Liam McGillivray wrote: Secondly, I found out that interfaces can't have variables. What!? That's crazy! Why wouldn't they? They totally should. Doesn't this mean that I will need to use getter and setter functions instead of direct access when using i

Re: Can a D library have some types determined by the client program?

2024-03-10 Thread Liam McGillivray via Digitalmars-d-learn
On Sunday, 10 March 2024 at 04:39:33 UTC, Liam McGillivray wrote: https://github.com/LiamM32/Open_Emblem/tree/templates-interfaces I will probably merge it into master soon. I have put up a merge request for these changes I have made to the library and the Raylib front-end. I would be interes

Re: Can a D library have some types determined by the client program?

2024-03-25 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 7 March 2024 at 22:18:40 UTC, Richard (Rikki) Andrew Cattermole wrote: There are two ways to do this. 1. Use templates. https://tour.dlang.org/tour/en/basics/templates Thank you for teaching me how to do this. This is where I first learned to use templates in D, and I have been