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

Liam McGillivray yoshi.pit.link.mario at gmail.com
Mon Mar 25 20:54:12 UTC 2024


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 using them since 
for functions, and as of yesterday, my first mixin template.

That being said, I'm pleased to announce the return of the `Map` 
class. As of yesterday's commit, the class template `class 
MapTemp(TileType:Tile, UnitType:Unit)` and `interface Map` have 
now been replaced with a single `Map` class as they were before.

At the time I had asked about this, I had not yet discovered that 
an array of objects can be filled with objects of a derived type 
by just using the regular cast syntax. Now that I know this, I've 
decided to just create the objects under the `VisibleTile` and 
`VisibleUnit` classes, place them in the `Map` object's arrays 
(cast to `Tile` and `Unit`), and then cast them back whenever I 
need to access anything specific to the derived classes.

Now I no longer have to deal with the limitations of interfaces. 
Things should be easier now.

The straw that broke the camels back was when I made my first 
mixin template, which adds functionality to a class for 
manipulating arrays of `Unit` objects. The compiler wasn't 
allowing it in the `Map` class because the array that I was 
trying to give it access to was of `UnitType`, not specifically 
the base `Unit` class. I'm happy to have it back.


More information about the Digitalmars-d-learn mailing list