Non-consistent implicit function template specializations

Rekel paultjeadriaanse at gmail.com
Tue Aug 17 14:20:00 UTC 2021


As my post was not the actual cause of my issue (my apology for 
the mistake), I think I have found the actual reason I'm 
currently having problems.
This seems to be related to a (seeming, I might be wrong) 
inability to specialize over both 1d and 2d arrays separately. 
(If constraining the length to values.)

This brings me to 2 questions.
1. How does one specialize a template for both 1d and 2d arrays 
at the same time?
2. Are there any plans to rework templates? like:
- Making `template TFoo(T : T[])` illegal. This makes little 
sense to me however I look at it, and as several people have 
advised against it it's confusing it's in the docs. (21.4.1)
- Changing the way priorities are set using specializations in 
general, to make them less pitfall-y.
- Allow for 2+d length specification. At the moment `T[][L]` 
works while `T[L][L]` does not seem to.

```d
void foo(T)(T a){...}

void foo(T:U[L], uint L)(T a){...}
void foo(T:U[L][L], uint L)(T a){...} // Never matched
void foo(T:U[L], U:V[L], V uint L)(T a){...} // Never matched 
(alternatively)

// Alternative workaround, very cluttery but with more control.
void foo(T)(T a) if(!is(typeof(a[0]))&&!is(typeof(a[0][0]))) {...}
void foo(T, uint L)(T[L] a) if(!is(typeof(a[0]))){...}
void foo(T, uint L)(T[L][L] a) {...} // Still does not work.
void foo(T, unit L(T[][L] a) {static foreach(i;0..L) 
assert(a[i].length==L); ...} // Will (generally) work but may 
include runtime checking & is generally much less preferrable.

// Should work with
void main(string[] args) {
	foo([[1],[2]]);
	foo([1,2]);
	foo(1);
}
```

- Supporting specializations such as the following (from 
https://forum.dlang.org/post/kdgfwlydkgmwvzrieyek@forum.dlang.org):
```d
void foo(L, T, uint S)(L l, T[S] r){
	writeln("foo");
}

void bar(L, R:T[S], T, uint S)(L l, R r){
	writeln("bar");
}

void main(string[] args) {
	foo(1, [1,2,3,4]); // "foo"
	bar(1, [1,2,3,4]); // "cannot deduce function"
}
```
I'm actually still not sure why this shouldn't work, but changing 
bar to foo isn't too difficult. (though it's more preferrable to 
work regardless, it currently feels like a pitfall)

---

Not sure if I'm asking the right questions, hope it's not of 
nuisance.
- Rekel


More information about the Digitalmars-d-learn mailing list