function vs. delegate in a unittest

Meta jared771 at gmail.com
Sun Apr 21 16:49:54 UTC 2019


On Sunday, 21 April 2019 at 08:52:40 UTC, Manfred Nowak wrote:
> Under dmd 2.085.1 this code compiles.
> But replacing the comment slashes by white space gives an error:
>   cannot implicitly convert expression
>     [...] delegate [...] to function [...]

Are you certain about that? Trying it with the "All dmd 
compilers" option on run.dlang.io seems to suggest that it won't 
compile with any version of dmd:
https://run.dlang.io/is/MdEpux

As per the language rules, your example can't work, because not 
marking `f` as static means that it will keep a context pointer 
to the unittest stack frame (unittests are really just functions 
that get automatically run).

Unless you mark it static, or there is some functionality 
implemented in the future where the compiler can statically 
determine that f doesn't access anything through its context 
pointer, and can therefore be converted to a `function` as 
opposed to a `delegate`, this won't work.


More information about the Digitalmars-d mailing list