Type Inference in @safe unittests

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 22 11:19:21 PDT 2017


On Tuesday, 22 August 2017 at 16:27:05 UTC, Jonathan M Davis 
wrote:
>
> Well, templates aren't the only case where we have attribute 
> inference anymore (e.g. auto return functions have it), and I'm 
> pretty sure that there have been several requests for fixing 
> issues regards to local declarations so that they have 
> inference (in particular, I think that there have been 
> complaints about marking a function as pure having issues with 
> internal declarations then not being treated as pure even 
> though they could be). And for better or worse, the trend has 
> been towards adding inference in cases where it's guaranteed 
> that the code will always be available and will be available to 
> any code using that code - and in the case of a declaration 
> inside of a function like that, it's guaranted that anything 
> referencing it is going to have access to the code. So, it 
> doesn't surprise me at all if attribute inference has been 
> added to local declarations like this. If you want to guarantee 
> that no inference is happening, then you'll probably have to 
> declare it directly in the module where it can't be infered due 
> to the fact that a .di file could redeclare it without any 
> function bodies.
>
> - Jonathan M Davis

Yeah, this happens with @safe main also (below), but not for more 
regular local blocks. Anyway, I found it very confusing as that's 
not how I assumed @safe applied to unittests or main worked.

@safe void main()
{
     struct Foo {
         int foo(int i, string s) @safe { return 0; }
         double foo2(string s) @safe { return 0; }
     }
     printMemberTypes!(Foo);
}


More information about the Digitalmars-d-learn mailing list