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

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Thu Mar 7 22:18:40 UTC 2024


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 delegate(string params) factory;

	this() {
         factory = (string params) {
			return new Tile;
		};

		foreach(i; 0 .. 10) {
			tiles ~= factory("");
		}
	}
}
```

The factory delegate is a very rough way to do it, there are other ways 
to describe it including an overridable method.

The design pattern: https://en.wikipedia.org/wiki/Factory_method_pattern


More information about the Digitalmars-d-learn mailing list