Why does compose from std.functional return a templated function

Jan Hönig hrominium at gmail.com
Wed Sep 16 09:59:59 UTC 2020


I have toyed with the compose template in std.functional and ran 
into some problems.
rikki_cattermole on discord helped me a lot to solve my problem.

However, what still remains (for me) to understand is why.

Source code for `compose`: 
https://github.com/dlang/phobos/blob/master/std/functional.d#L1161



`compose` pipes together given functions. And returns a TEMPLATED 
function.
Why does it return a templated function? At compile-time, compose 
definitly knows, what kinds of function it composes together. So 
with std.traits, it could put there a definitve type, depending 
on the given function(s).

I somewhat see, that inside compose itself, this probably solves 
some issues with casting (maybe).
However, the last composition, i.e. the one which is returned, 
does not need to be templated, since it is known, what parameter 
has the last function.

In my case, I failed to understand, that it returns a 
non-initialized template function, which lead into compile 
problems.

In general I can imagine that this leads to weird compile errors, 
which are hard to understand. (types, casting, etc.)


My main question is why? Is there something, which I am missing, 
that explains, why it is beneficial to return a templated 
function?

(maybe, because I might want to compose together templated 
non-initialized functions?)


More information about the Digitalmars-d-learn mailing list