Function template declaration mystery...

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Feb 28 21:02:33 UTC 2018


On Wed, Feb 28, 2018 at 09:36:33PM +0100, Robert M. Münch via Digitalmars-d-learn wrote:
[...]
> Yes, that's what the docs state. And I can imagin this. Bit this
> sentence is a bit hard to understand: "If fun is not a
> string, unaryFun aliases itself away to fun." Whatever this means.
[...]

That means no overhead is introduced if `fun` is not a string. When
`fun` is a string, unaryFun needs to create a lambda that implements the
operation described in the string, and this may involve a GC allocation.
If `fun` is already a function, delegate, or lambda, though, it can be
used directly, so there's no need to incur the overhead of creating yet
another delegate to wrap around `fun`.  The way this is done is by
unaryFun defining itself to be an alias to `fun` in this case, so that
all references to unaryFun with that argument essentially becomes
references to `fun` instead.

It's really an implementation detail of Phobos that users don't really
need to know, but I suppose the intent was to reassure the user that no
unnecessary overhead will be incurred where it's avoidable.


T

-- 
If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell


More information about the Digitalmars-d-learn mailing list