Thread overview
A shorter alias for scope(exit) possible? a case for `defer`
5 days ago
ryuukk_
5 days ago
ryuukk_
5 days ago
zjh
4 days ago
Nick Treleaven
4 days ago
ryuukk_
5 days ago

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?

5 days ago

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

5 days ago

On Saturday, 6 August 2022 at 00:23:48 UTC, ryuukk_ wrote:

>

...

You can use vim-snippets etc to help you type.

4 days ago

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.

4 days ago

Indeed custom snippet is a good idea! I should start to do that more