Live data from Hacker News

Ask HN: What should have been the term for RAII?

news.ycombinator.com

31–40 of 62 posts

Re: Ask HN: What should have been the term for RAII?

#31
post #6

Earlier quoted context omitted.

Are there languages that do SBRM but not RAII?

Yes. Eg if you look at Java try-with-resources there is an exact scope during which resources live and can be used. At the end of the scope the .close() method will be called. For C++ and Rust there might not be a well defined scope since objects can be „moved“. Eg a method constructing an object can return it to the caller without the destructor being called.

It's well-defined, it's just more flexible than Java. The difference is that returning a value moves it, and resources are owned by the creating scope by default. Java doesn't have the same concept of ownership, or rather, you can't move values in Java.

Re: Ask HN: What should have been the term for RAII?

#34
post #6
post #5

Earlier quoted context omitted.

RAII isn't just about automatic ("scope-based") variables. RAII refers to the way the language ties together the construction and allocation of the object.

Are there languages that do SBRM but not RAII?

Plenty of languages don't have RAII have SBRM. Scope based resource management predates the idea of constructors by decades. It's common in Lisp and Scheme. Haskel doesn't have RAII, but scope-based resource management is critical.

``` (call-with-output-file some-file (lambda (out) (write 'hello out))) ```

Re: Ask HN: What should have been the term for RAII?

#36
post #6
post #5

Earlier quoted context omitted.

RAII isn't just about automatic ("scope-based") variables. RAII refers to the way the language ties together the construction and allocation of the object.

Are there languages that do SBRM but not RAII?

D has constructs:

    scope(exit) foo(); // Call foo at the end of the scope
    scope(success) foo(); //Call foo if all goes well
    scope(failure) foo(); //Call foo if the wings fall off the plane.

Re: Ask HN: What should have been the term for RAII?

#37
post #15

Earlier quoted context omitted.

Unfortunate acronym, you're going to get a bunch of lisp docs...

You are right, I do program in LISP and love the C[AD]+R idea. I even had my own proposal 5 years ago for improving that ! > That's right, but I would go one step further and have F(irst) and R(est), with obvious composition as follows: (using kruhft's examples from another thread) > (ff x) == (caar x) == (car (car x)) > (rrf x) == (cddar x) == (cdr (cdr (car x))) > I would argue that it's worth sacrificing the 'f' a…

The only problem is that the car and cdr are not the first and rest of anything, when they are just used for tree structure.

Only when the tree structure conforms to certain conventions and intent of representing a list is the car "first" and the cdr "rest".

So of course those names are fine for nested lists: (ff '((a b c) d)): the "first of the first". But in, say, an assoc list ((a . 3)) 3 is not the "rest" of anything; it's the value of the key a.

The proposed functions go with the first and rest functions, rather than replace the cddr ones.

Now let's talk about something else: the order. In caddr, the order is just a condensation of the nested application of (car (cdr (cdr ...))), in the same order: it's easy to convert between the two, both actually and mentally. However, in left to right threading syntax, it's backwards:

   (flow value car car cdr (+ 1))
corresponds to

   (+ 1 (cdr (car (car value))))
so it condenses like

   (flow value cdaar (+ 1))
You can see someone wanting a variant which has the letters in the opposite order.

With f and r functions, you can do:

   (flow value f f r (+ 1))
which is almost the backwards "ffr" you might want.
Post reply on HN