www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template + alias + basic type depends on another parameter = broken?

reply Dark Hole <dark.hole1 yandex.ru> writes:
I'm trying to rewrite really old D code. There was fragment like 
that:
```d
template Foo(T, T[] Array) {
     // ...
}
// ...
Bar[] arr;
Foo!(Bar, arr);
```

This gives error `can't read arr in compile time`. Small changes:
```d
template Foo(T, alias T[] Array) {
     // ...
}
// ...
Bar[] arr;
Foo!(Bar, arr);
```

This is valid D, but it doesn't work. It gives error "Error: 
template instance `Foo!(Bar, arr)` does not match template 
declaration `Foo(T, alias T[] Array)`".

Of course, there is some ways to avoid this error (e.g. check 
Array type in if), but I don't undestand why this code doesn't 
compiles.
Feb 22 2023
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Wednesday, 22 February 2023 at 20:20:46 UTC, Dark Hole wrote:
 ```d
 template Foo(T, alias T[] Array) {
     // ...
 }
 // ...
 Bar[] arr;
 Foo!(Bar, arr);
 ```

 This is valid D, but it doesn't work. It gives error "Error: 
 template instance `Foo!(Bar, arr)` does not match template 
 declaration `Foo(T, alias T[] Array)`".

 Of course, there is some ways to avoid this error (e.g. check 
 Array type in if), but I don't undestand why this code doesn't 
 compiles.
Pretty sure this is just a bug. There are a lot of edge-case bugs like this where template instantiation doesn't work the way it's supposed to. I've submitted a bug report for this on issues.dlang.org: https://issues.dlang.org/show_bug.cgi?id=23733
Feb 22 2023
prev sibling parent Elfstone <elfstone yeah.net> writes:
On Wednesday, 22 February 2023 at 20:20:46 UTC, Dark Hole wrote:
 I'm trying to rewrite really old D code. There was fragment 
 like that:
 ```d
 template Foo(T, T[] Array) {
     // ...
 }
 // ...
 Bar[] arr;
 Foo!(Bar, arr);
 ```

 This gives error `can't read arr in compile time`. Small 
 changes:
 ```d
 template Foo(T, alias T[] Array) {
     // ...
 }
 // ...
 Bar[] arr;
 Foo!(Bar, arr);
 ```

 This is valid D, but it doesn't work. It gives error "Error: 
 template instance `Foo!(Bar, arr)` does not match template 
 declaration `Foo(T, alias T[] Array)`".

 Of course, there is some ways to avoid this error (e.g. check 
 Array type in if), but I don't undestand why this code doesn't 
 compiles.
I never used really old D compiler, but the error looks reasonable to me. The following code should compile - note arr is "const". template Foo(T, T[] arr) { void boo() { } } struct Bar { } void main() { const Bar[] arr;// = []; (Foo!(Bar, arr)).boo(); }
Feb 23 2023