How can I have those "template instance recursive expansion" errors under control?

realhet real_het at hotmail.com
Sun Dec 1 19:55:59 UTC 2024


On Sunday, 1 December 2024 at 00:08:02 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> The trick...

I've managed to 'fix' it.  Sadly it's not a rational fix, just 
voodoo magic :D

When I mixin() my aliases for my Vector types, I have to 
'mention' all types first, because if I use the simple alias name 
from a different module, inside the complicated image2D template, 
the compiler reaches a limit and gives up. (I'm sure it's not an 
infinite limit, although I have no data on this, it just works 
with the magic trick.)

```d
static foreach(T; vectorElementTypes)
static foreach(N; vectorElementCounts)
{
	/+voodoo magic ->+/pragma(msg, Vector!(T, N).stringof[0..0]);
	/+
		Note: BugFix: 241201: Without 'mentioning' the vector type 
first,
		/+Code: image2D(1, 2, RGB(1, 2, 3))+/ drops a template instance 
recursion rerror
		at random places when it tries to resolve the RGB alias.
		❗ Must be placed in front of the alias declaration!
		❗ Can't use __traits(compiles, ...) or static assert(...) or 
mixin(dummy function with variable declaration)
			because those fail with various errors like: "forward 
reference" error.
	+/
	mixin(iq{alias $(Vector!(T, N).VectorTypeName) = $(Vector!(T, 
N).stringof); }.text);
}
```

So this feels like a bug, but it has no simple case to reproduce, 
it needs a big complexity to fail.

Recap:

This fails in a separate module:
```d
import het.math; void main(){ auto img = image2D(1, 2, RGB(1, 2, 
3)); }
```

This is one ugly fix:
```d
import het.math; void main(){ auto img = image2D(1, 2, 
Vector!(ubyte, 3)(1, 2, 3)); }
//not using the nice alias name: RGB
```
This is another ugly fix:
```d
import het.math; void main(){ auto col = RGB(1, 2, 3); auto img = 
image2D(1, 2, col); }
```

This is the voodoo magic fix:
```d
import het.math; void main(){ pragma(msg, Vector!(ubyte, 3));auto 
img = image2D(1, 2, col); }
```

And if I write the pragma(msg,) into the het.math module, I'm 
able to express my thoughts with minimal redundancy:
```d
import het.math; void main(){ auto img = image2D(1, 2, RGB(1, 2, 
3)); }
```

This is where I started hobby programming with a positive mood 
yesterday... I tried a simple thing and it failed... But anyways, 
a matrix of template types are not simple stuff, not for me, 
neither for the compiler :D


More information about the Digitalmars-d-learn mailing list