www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A shorter alias for scope(exit) possible? a case for `defer`

reply ryuukk_ <ryuukk.dev gmail.com> writes:
I'm not a RAII lover

I like to manage manually manage resources and have fine control 
over them

So i use `scope(exit)` a lot, however they can be a pain to type, 
mostly due to the parenthesis

The first thing i tried was an alias, but unfortunately that's 
not possible

```
src\states\gameplay_r.d(35,20): Error: basic type expected, not 
`(`
src\states\gameplay_r.d(35,20): Error: function declaration 
without return type. (Note that constructors are always named 
`this`)
src\states\gameplay_r.d(35,26): Deprecation: storage class 
`scope` has no effect in type aliases
```

And even if possible, i'm still not a fan of having to import a 
module everywhere i'd need to type it

What do you guys think? `defer` seems to be a the choice in most 
of the languages, could it be a good alias for `scope(exit)` in 
D? if no what's your solution?
Aug 05 2022
next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
One argument against `defer` alias is D's way is actually more 
complete:  https://tour.dlang.org/tour/en/gems/scope-guards

So introducing an alias might look weird
Aug 05 2022
parent zjh <fqbqrr 163.com> writes:
On Saturday, 6 August 2022 at 00:23:48 UTC, ryuukk_ wrote:
 ...
You can use `vim-snippets` etc to help you type.
Aug 05 2022
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Saturday, 6 August 2022 at 00:16:56 UTC, ryuukk_ wrote:
 I'm not a RAII lover

 I like to manage manually manage resources and have fine 
 control over them
Vale has a feature where it won't let you forget to call a function to destroy user types marked as such when they go out of scope. So you call a named destructor, which can take arguments too: https://verdagon.dev/blog/higher-raii-7drl I'd like this in D. It doesn't replace scope(exit), but it guarantees you don't forget to do 'manual' cleanup.
 So i use `scope(exit)` a lot, however they can be a pain to 
 type, mostly due to the parenthesis
Personally I would use snippet completion in my editor (Geany). Might be called something else in other editors. So when I type `se` and press tab it would expand to `scope(exit)`. You can configure it to put a brace block after it if you want and have the cursor be inside it with appropriate indentation.
Aug 06 2022
parent ryuukk_ <ryuukk.dev gmail.com> writes:
Indeed custom snippet is a good idea! I should start to do that 
more
Aug 06 2022