what are the rules for @nogc and @safe attributes inference?

ikod geller.garry at gmail.com
Fri Nov 16 10:25:26 UTC 2018


On Thursday, 15 November 2018 at 21:55:18 UTC, Steven 
Schveighoffer wrote:
> On 11/15/18 4:09 PM, Adam D. Ruppe wrote:
>> On Thursday, 15 November 2018 at 21:00:48 UTC, ikod wrote:
>>> what are the rules for @nogc inference?
>> 
>> It attempts it if and only if it is a template.
>
> Well, the general "rule" is, if it's code that must be 
> available to the compiler when it's called, then it will be 
> inferred.
>
> Examples of code that must be processed every time it's used:
>
> 1. Template functions
> 2. auto-returning functions
> 3. functions inside templates (like member functions of a 
> templated struct)
> 4. Inner functions
>
> There may be others I didn't think of.
>
> Everything else must be manually attributed. The reasoning is 
> that the function may be stubbed in a .di file, and in that 
> case, attribute inference wouldn't be possible.
>
> -Steve

Thanks for clarifications, Adam and Steven!

My problem is next code:
=================================================
import std.traits;

T library_func(T)(/*lazy*/ T i)
{
     // this fuction can be @nogc or not depending on T properties
     static if (isArray!T) {
         return i ~ i;
     }
     else
     {
     	return i;
     }
}
void user_function_nogc() @nogc
{
     int x = 1;
     library_func(x+1);
}
void user_function_gc()
{
     library_func([1]);
}
void main()
{
}
=====================================================
This code compiles as long as `lazy` is commented out. But I'd 
like to have
both lazy parameter and @nogc inferrence for `library_func` so 
that user is not locked to code @nogc or not.



More information about the Digitalmars-d-learn mailing list