r/ProgrammingLanguages • u/mark-sed github.com/mark-sed/moss-lang/ • 23d ago
Discussion Do you need a language to have context managers (with/using directive)?
In my language I was avoiding adding with or using directive for handling context of objects and tried to rather use a functional approach such as:
=
But this does not always works or is convenient since often I need to do additional operations on the file afterwards. So lately I more and more lean towards actually adding support for using, otherwise I fear people would re-invent it anyway using try-finally blocks.
In this case in my language I would have to create a new function to do these 3 operations since lambdas can have only 1 expression:
, =
So my question to you is, if you think that having context managers is really useful and a good addition to a language or rather an extra feature that can be avoided?
23
u/PikachuKiiro 23d ago
why
30
u/latkde 23d ago
Pragmatically, any non-pure language will need some kind of deterministic resource management feature. Pick one of these:
deferstatementswith-statements /using/try-with-resourceA try-finally is generally not sufficient because the place that cleans up the resource tends to be far away from the place that acquires the resource, these should always be as close as possible. Also, it's easy to forget required cleanup steps if it's possible to acquire a resource through a mechanism that doesn't guarantee cleanup. Thus, defer-statements aren't a great idea either in new languages.
Your language sketch feels unsatisfactory because you've artificially restricted lambdas to a single expression. But even then, resource management via callbacks can be tricky when combined with other control flow, e.g. early returns out of a surrounding function, or breaking out of a surrounding loop. Ruby addresses this by having different kinds of nested functions that interact with control flow differently. In an imperative language that features control flow, introducing dedicated resource scope syntax will generally be more helpful.
6
u/perlgeek 22d ago
I've been doing quite a bit of programming in both Python (which has context managers) and Perl (which doesn't), and as a user I really like having context managers. It makes the lifetime of certain things (like file handles, in the
opencase) very obvious.6
u/tdammers 22d ago
Honestly, the difference between
with_openandusingin your examples is just an unpleasant consequence of the artificial distinctions between a "function" and a "block of code".In a language that supports proper first-class anonymous functions (or, more accurately, procedures), your
usingexample could just be written as:To answer the actual question though: yes, I am 100% convinced that in a typical (imperative) language, some sort of resource management feature is necessary.
But it doesn't necessarily need to be a core language feature. If people re-invent this particular wheel properly, a sound
with-abstraction on top of your language's exception machinery that's straightforward and safe to use, and that thing becomes a de facto standard function, then I don't see a problem with that at all.In fact, this is exactly how it works in Haskell. The language itself doesn't do anything special to make this possible, but the
bracketfamily of functions is widely used for this exact purpose, and they are implemented as plain Haskell function, using Haskell's exception handling features behind the scenes. Here's what that would look like:But you would rather actually just use
withFile, which is another abstraction on top ofbracket, like this:So yeah, you want to make this stuff possible, convenient, and obvious, but depending on how your language works, the bracketing itself doesn't necessarily need to be a core language feature.
6
u/MrJohz 22d ago
One thing that I've not seen people mention here yet is that all of your examples here involve indentation — the
using/with_open/whatever always creates a new scope, it can't be part of an existing scope. So something like:That second indent is completely unnecessary — there's nothing going on before the
usingthat we need to create a new scope for.JavaScript's
usingsyntax is simple assignment for this reason:This will close
fwhen the current scope (in this case the function body) ends. And you can create arbitrary scopes by wrapping code in blocks, if there is a situation where you need to be more explicit about scoping:I believe C#'s
usingcan take this form in addition to the block form. In general, as long as you can create explicit scopes, havingusingjust be a kind of variable declaration is exactly as powerful as the other form, and in my experience it is consistently nicer to write.Another approach is found in Gleam (and some other languages):
useexpressions. Take thewith_openexample in the OP. That could be rewritten withuseas:Basically, everything that follows the
use fline gets turned into an anonymous function body, and thewith_open()call gets that anonymous function as the final argument. This also eliminates indentations, but it has the advantage that the syntax is significantly more general. Rather than eliminating one specific type of indentation, this approach can flatten out all callbacks in general.2
u/initial-algebra 22d ago
The problem with
usingand equivalentwith*is that it forces resources to be cleaned up in reverse order, which is not always appropriate. Destructors are better.