Eponymous template with static if

Michael Galuza riddlermichael at gmail.com
Thu Aug 10 14:34:20 UTC 2023


On Wednesday, 9 August 2023 at 18:28:38 UTC, sighoya wrote:
> Giving it an explicit type make it workable

Of course, but such usage of a template functions is... 
useless:-) I want type argument `T` will be inferred.

Anyway, I wondered why compiler rewrite `square!(int)(3)` as 
`square!(int).square(3)`, but in the same time it's not so clever 
to eliminate `!(int)`. I can only guess that type inference is 
impossible in this situation. I mean in ordinary cases compiler 
can match literal `3` and type `int` directly, but what it should 
do in such simple case:
```
template square(T) {
     static if (is(T)) {
         T square(T t) {
             return t * t;
         }
     }
}
```
Of course compiler could conclude that expression `square(3)` is 
correct only if `static if` branch was successful, so `is(T) == 
true && T == int` (hi, Hindley–Milner :-), but such type 
inference require complex analysis "bottom-up", from template 
members to template itself, whereas we analyse template body 
"up-down", from template args and static if-s to members.

Of course all above is just my poor and miserable tryings to 
understand compiler's pranks, don't judge me.


More information about the Digitalmars-d mailing list