destroy and @safe

Steven Schveighoffer schveiguy at gmail.com
Tue Jun 21 15:14:43 UTC 2022


On 6/21/22 10:40 AM, Antonio wrote:
> I'm using explicitly destroy!false(obj) for a "deterministic" resources 
> release.
> 
> I replicate the c# "using" pattern, or the python "with" pattern with my 
> own "use" template supposing object are RAII
> 
> i.e.:
> 
> ```d
> Item[] items = query("...").use( (Answer a) =>
>    a.rangify.map!(rowToItem).array()
> );
> 
> ```
> 
> The problem:
> 
> "use" can't be @safe because it contains a call to "destroy".
> 
> For better understanding of the idea, I include the "use" template code
> 
> ```d
> R use(R, T)(T obj, R delegate(T) fT)
> {
>    scope (exit)
>      destroy!false(obj);
> 
>    return fT(obj);
> }
> ```
> 
> What's the way to ensure @safe using destroy? (if possible)

destroy is @safe if the destructor is @safe.

But your `use` function is being inferred as @system. Why it's being 
inferred is likely not the destroy call (or maybe it's not the only 
problem). You delegate doesn't seem to be marked @safe as well.

To find the problems, mark `use` as @safe, and see what it says.

-Steve


More information about the Digitalmars-d-learn mailing list