Please, can the langauge stop inferring scope and return for @trusted/@system?

Dukc ajieskola at gmail.com
Thu May 19 09:44:29 UTC 2022


On Thursday, 19 May 2022 at 09:29:50 UTC, Dennis wrote:
> I also don't agree with the "principle of least astonishment" 
> violation. Consider this slice example:
>
> ```D
> struct Slice(T)
> {
>     size_t length;
>     private @system T* _ptr;
>
>     // `@safe` read-only access to _ptr
>     T* ptr() @trusted { return length == 0 ? null : _ptr; }
> }
> ```
>
> I expect the compiler to infer `return scope` for `ptr()` here.

Sorry, I don't think that can be helped. The compiler can infer 
simple cases like this, but then it has to draw a line in water 
where it stops the inference as the function body gets more 
complex. You're going to be surprised anyway at some point if you 
expect inference from a `@trusted` function.

It would probably be reasonable to have the compiler error in 
cases like this. But if the function compiles, there should be 
nothing implementation dependent about `scope`, `return scope` or 
lack thereof.


More information about the Digitalmars-d mailing list