RFC: scope and borrowing

Ivan Timokhin via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 4 11:12:57 PDT 2014


04.10.2014 21:01, Ivan Timokhin пишет:
> 04.10.2014 17:38, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm at gmx.net>" пишет:
>>
>> I think the key is in separating the scope attribute and the owner. The
>> former needs to be part of the type, the latter doesn't. In this vein,
>> it's probably a good idea to restrict the `scope!owner` syntax to
>> function signatures, where it may only refer to other parameters and
>> `this`. The use cases for it elsewhere are very marginal (if they exist
>> at all).
>>
>> This naturally makes the return type of `findSubstring()` just
>> `scope(string)`; declaring a variable of it simply works:
>>
>>      typeof(findSubstring("", "")) s = findSubstring("Hello, world",
>> "world");
>>
>> is equivalent to:
>>
>>      scope(string) s = findSubstring("Hello, world", "world");
>>
>> This is a valid assignment, and owner propagation would even take care
>> of preserving the owners (though only on declaration, but that's natural
>> because the owners are only known at the call site):
>>
>>      string haystack, needle;
>>      scope(string) s = findSubstring(haystack, needle);
>>      // type of `s` is scope(string), owner is `haystack`
>>
>> In other words, the type part of `scope!a(T)` is just `scope(T)`, the
>> owner is not part of the type and tracked separately.
>>
>
> That's ok with me (in fact, it looks very nice), but I think Manu's
> point was that dealing with anything that isn't part of the type is
> troublesome.
>

On the second thought, doesn't syntax look a bit awkward now? I mean, 
`scope!a(T)` certainly looks like `scope!a` as a whole is a type 
modifier. Since this syntax is pretty much limited to function return 
types with this proposal, maybe a better solution would be to have an 
owner as a separate annotation on a function?

Also, would it really make much sense to track the owner further than 
the assignment of a function's return value? That seems to complicate 
things a lot by adding a hidden attribute to a variable that is not only 
invisible at the declaration (even though the full type is spelled out), 
but, in fact, cannot be specified explicitly (because there's no syntax 
for that, now that scope with owners is limited to function signatures).

How about this:
---
     scope(string) haystack, needle;
     // next assignment is okay, because `s` is guaranteed not to outlive
     // `haystack`.
     scope(string) s = findSubstring(haystack, needle);
     // type of `s` is now scope(string), no additional information
     // attached

     // so the next assignment is disallowed:
     //needle = s; // error!
---

This could be unnecessarily limiting, but would it really cause much 
trouble?



More information about the Digitalmars-d mailing list