On Fri, Jul 31, 2026 at 08:10:39AM -0700, H. S. Teoh via Digitalmars-d-learn 
wrote:
> > On Friday, 31 July 2026 at 13:52:12 UTC, Vindex9 wrote:
> > > Is this code normal? Why can't it be marked with `@safe`? Can only
> > > `idup` be used for `@safe` in such cases?
> > > 
> > > ```d
> > > string fn() @trusted pure {
> > >     char[] arr;
> > >     // some manipulations...
> > >     return cast(string) arr;
> > > }
> > > ```
> > > 
> > > Where is the line between cases where the `@trusted` tag can be
> > > used and cases where the `@system` tag can be used?
> [...]
> 
> If the function is pure, you should be able to just return char[] and
> the caller will be able to assign it to string.  No need for a cast.
> Just specify the return type as `char[]`.
[...]

Proof of concept:

```
import std;

char[] pureStringFunc() pure @safe {    // N.B. returns mutable char[]
        char[] arr;
        arr ~= "abc";
        foreach (i; 0 .. 5) {
                arr ~= i.to!(char[]);
        }
        return arr;
}

void main() @safe {
        string s = pureStringFunc();    // N.B. assigns to (immutable) string
        writeln(s);
}
```

Output:
```
abc01234
```


T

-- 
How many guacas are in 1 guacamole?  6.022*10^23, also known as Avocado's 
Number.
  • cast(string) for... Vindex9 via Digitalmars-d-learn
    • Re: cast(st... user1234 via Digitalmars-d-learn
      • Re: cas... H. S. Teoh via Digitalmars-d-learn
        • Re:... H. S. Teoh via Digitalmars-d-learn
      • Re: cas... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: cast(st... Nick Treleaven via Digitalmars-d-learn
    • Re: cast(st... Kagamin via Digitalmars-d-learn
      • Re: cas... Kagamin via Digitalmars-d-learn
        • Re:... Vindex9 via Digitalmars-d-learn
          • ... Jonathan M Davis via Digitalmars-d-learn
          • ... Vindex9 via Digitalmars-d-learn

Reply via email to