function vs. delegate in a unittest

Bastiaan Veelo Bastiaan at Veelo.net
Sun Apr 21 20:41:17 UTC 2019


On Sunday, 21 April 2019 at 08:52:40 UTC, Manfred Nowak wrote:
> On Sunday, 21 April 2019 at 06:39:37 UTC, rikki cattermole 
> wrote:
>>     static void func() {
>
> thy for the hint, that one can use `static' to virtually lift 
> the definition of a function up to the local root of the 
> hierarchy, which makes my oversimplified argument invalid for 
> unittests.
>
> But this seems not to solve the underlying problem:
>
> unittest{
>          void f (){}
>   static void fs(){}
>   struct S( T){
>     T* f;
>     this( T)( T fl){
>       f= fl;
>     }
>   }
> //auto sf = new S !( typeof( f ))( &f );
>   auto sfs= new S !( typeof( fs))( &fs);
> }
>
> 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 [...]
>
> So that's not a problem of unittests but of templates?

This works:

unittest
{
          void f (){}
   static void fs(){}
   struct S( T){
     T f;
     this( T)( T fl){
       f= fl;
     }
   }
   auto sf = new S !( typeof( &f ))( &f );
   auto sfs= new S !( typeof( &fs))( &fs);
}

https://run.dlang.io/is/yu67oN

And regarding "requiring coders to manage different versions for 
both types of functions": there is std.functional.toDelegate, so 
if you just support delegates, that is enough -- unless you care 
about @safe.

Bastiaan.


More information about the Digitalmars-d mailing list