Type Inference in @safe unittests

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 22 14:55:44 PDT 2017


On 8/22/17 5:44 PM, jmh530 wrote:
> On Tuesday, 22 August 2017 at 18:25:31 UTC, Steven Schveighoffer wrote:
>>>
>>> @safe void main()
>>> {
>>>      struct Foo {
>>>          int foo(int i, string s) @safe { return 0; }
>>>          double foo2(string s) @safe { return 0; }
>>>      }
>>>      printMemberTypes!(Foo);
>>> }
>>
>> The surprising part to me is that non- at safe main doesn't infer 
>> anything. Is that true?
>>
> 
> They aren't auto functions or templates, just normal member functions of 
> a struct in main. I thought inferring function attributes was only for 
> auto functions and templates. I never thought it would for member 
> functions of structs in main. But yeah, I'm pretty those functions are 
> @safe and not @safe @nogc nothrow pure.

Logically, any internal struct's code is available for examination 
wherever it could possibly be used. If it's returned, then the function 
return must be auto (and the function code available). If it's passed as 
a template parameter, it is available.

Inference could happen on any internal function anywhere, not just 
inside internal structs.

I'm not sure why the marking of the enclosing function should make a 
difference, either they all infer or they all don't infer. To me, this 
seems like a bug (the inconsistency).

-Steve


More information about the Digitalmars-d-learn mailing list