On Borrow Checking

Dukc ajieskola at gmail.com
Fri May 16 11:20:40 UTC 2025


On Friday, 16 May 2025 at 01:27:18 UTC, Manu wrote:
>
> Don't write `scope` if you don't want it? What are the cases 
> where this is a struggle?

Lets say you have a function `int* fun(return scope int* arg, 
uint flags)`.

You're calling it like

```D
enum enumVal1 = /*...*/, enumVal2 = /*...*/, /*...*/, enumVal42 = 
/*...*/;

@system callingFunction()
{   int[16] localInts;
     // initialise localInts...
     static int* cache;
     if (cache == null)
     {   cache = fun(&localInts[enumVal1], enumVal12 | enumVal36);
     }
}
```

Under the current scope checking rules, `result` is going to be 
inferred as `scope`, because a scope argument 
(`&localInts[enumVal1]`) is assigned to a `return scope` 
parameter. Therefore, assigning it to a static variable wouldn't 
compile with scope checking on.

However, it might be the case that `result` is not actually 
pointing to the stack. Let's say `fun` will return `arg` only if 
a certain bit in `flags` is set, and the call in 
`callingFunction` is leaving that bit unset. In that case, the 
scope check would be returning a false error that you might want 
to override.



More information about the Digitalmars-d mailing list