Live data from Hacker News

Rust RAII is better than the Haskell bracket pattern

snoyman.com

141–150 of 156 posts

Re: Rust RAII is better than the Haskell bracket pattern

#141
post #108

Earlier quoted context omitted.

> What are you talking about? Please edit swipes like that out of your comments here. The rest is fine and stands on its own.

I would love to edit that out but the two-hour edit window is so short, sometimes.

I've reopened it for editing if you still want to do that.

Re: Rust RAII is better than the Haskell bracket pattern

#142
post #33

Earlier quoted context omitted.

If there's a cycle of strong references with Rc or Arc (or shared_ptr in C++), those objects still never get dropped/have their destructors called.

I think that falls into the category I mentioned in the third paragraph of my comment: a serious pre-existing bug with other consequences will potentially cause the guarantee to be violated. A similar effect would happen if you had a double free that sometimes caused a crash, which is a similar level of programming mistake to creating a cyclic reference. To me it sits outside of a reasonable definition of "guaranteed…

No, typically, a reference cycle is fine. It results in valid memory that never gets read again, which is unfortunate but not dangerous, whereas double-frees can result in memory corruption. http://huonw.github.io/blog/2016/04/memory-leaks-are-memory-...

Re: Rust RAII is better than the Haskell bracket pattern

#143
post #117
post #33

Earlier quoted context omitted.

If there's a cycle of strong references with Rc or Arc (or shared_ptr in C++), those objects still never get dropped/have their destructors called.

Rc's drop will be called. But whether the exposed object's drop will be called is dependent on the reference count. But Rc would not work if the drop was not guaranteed to be called.

I was a little unclear but that is of course what I meant: talking about the underlying shared data because the pointers themselves don't have particularly interesting destruction behaviour. (Although the sibling is also correct that not all Rc/Arc/shared_ptr handles to the shared data with have their Drop called.)

Re: Rust RAII is better than the Haskell bracket pattern

#144

Earlier quoted context omitted.

I only mean it's visibly more obvious, you have an indented block... what is the purpose of the indented block unless to say "do all your stuff with the resource _inside_ this block". Using the with block is very 'intentional' feeling. I'm not very familiar with Haskell but it seems like you'd get used to the type system telling you everything you need to know. But in this case it doesn't. In Python world we talk abo…

> I only mean it's visibly more obvious, you have an indented block... Haskell is more similar than you realise, it's the difference between this: withSomeResource $ \resource -> do someFunctionOn resource and this: with some_resource() as resource: some_function_on(resource) > I'm not very familiar with Haskell but it seems like you'd get used to the type system telling you everything you need to know As an outsider…

Right. I mean, consider your simple python:

    with some_resource() as resource:
        some_function_on(resource)
Is that broken? If some_function_on saves the resource, yes. If it just temporarily uses it, no.

I don't think the claim that it's syntactically obvious in Python is correct. In both cases the typical syntax helps a little but it's easy to get wrong.

It is the case that "the typical syntax" is a little more enforced by Python-the-language.

Re: Rust RAII is better than the Haskell bracket pattern

#146
post #35

Earlier quoted context omitted.

Please, don't add them to the language. Use the library approach instead, it is much more Haskellish.

Haskell has such an extensive set of language extensions, I would say adding new features to the type system is probably the MOST Haskell-ish way of doing things. The explicit purpose of Haskell is to be a basis for research into functional language design (edit: among other purposes). By "explicit purpose" I mean exactly that... people got together in 1987 to come up with a language for research. Haskell was never s…

They are fixing omissions related to full dependent types, many of them.

Compare language features and Haskell's approach: Erlang and distributed-process, goroutines and channels and Control.Concurrent(.Chan), (D)STM is a library, Control.Applicative and Control.Monad for many things hardly expressible in any other language, etc, etc.

Linear types, I am afraid, would go the way implicit parameters went - their use is cumbersome and they really do not help much with day-to-day programming and when they are needed they can be perfectly implemented with a library.

Re: Rust RAII is better than the Haskell bracket pattern

#147
post #106
post #35

Earlier quoted context omitted.

Please, don't add them to the language. Use the library approach instead, it is much more Haskellish.

From a language design perspective it makes a lot of sense to add linear types to the language itself instead of using an encoding. Every encoding that I know of (such as region types encoded as monads, which is what I think the article wants to get at) leads to excessive sequentialization of code. This in turn leads to a lot of boilerplate (or crazy type inference problems) at compile time as well as suboptimal run…

Have you took a look at Clean the programming language? It has unique types (used for resource management, but less restrictive than linear types) for decades and guess what? They invented special syntax (the "#-notation") which introduce shadowable names much like regular monad syntax does. And code with this syntax is, basically, sequential code most of the time albeit unique types allow for something like "where" clause. You just easily get lost with these handle, handle1, handle2... names.

I do not oppose inferring linear use at core and/or intermediate representation (GRIN allowed for that and more). I just do not see their utility at the high level, in the language that is visible to the user.

Re: Rust RAII is better than the Haskell bracket pattern

#148
post #35

Earlier quoted context omitted.

Please, don't add them to the language. Use the library approach instead, it is much more Haskellish.

It's extremely difficult to do this and maintain even the figment of usability. Unless, of course, you're implying it's very haskellish to implement libraries with huge usability gotchas (of which ResourceT was one until the Ghosts of Departed Proofs paper reminded us we can reuse the machinery of ST), then I totally agree.

There was an abstract of PhD thesis devoted to enhancing usability of DSeLs by helping with error messages - they had to be expressed in terms of DSeL, not Haskell-the-language. And linear types as a library (be it ResourceT or something other) is a DSeL.

I think it is a better venue which can help many applications simultaneusly. While linear types won't.

Re: Rust RAII is better than the Haskell bracket pattern

#149
post #35

Earlier quoted context omitted.

Please, don't add them to the language. Use the library approach instead, it is much more Haskellish.

What is "the library approach"? Library approaches have failed. See http://www.well-typed.com/blog/2016/09/sharing-conduit/

Look at distributed-process. It is Erlang-in-Haskell without changing the language.

So library approach thrives.

Re: Rust RAII is better than the Haskell bracket pattern

#150

Earlier quoted context omitted.

As I mentioned in my other reply to Steve Klabnik, documenting this edge case would have been a sufficient “resolution” to the bug at hand. I may call it a bug, and you may call it undocumented silent data loss behavior; either way, we’re talking about the same thing. Silent data loss from undocumented behavior is not good, wouldn’t you agree? I certainly was not aware that drops in Rust could throw away potentially…

I mean, I guess it's not documented as well as it could be, but the only alternative to dropping close errors would be panicking, and that would be significantly worse. Destructors aren't supposed to fail.

> the only alternative to dropping close errors would be panicking

I think it could take a callback, so it could note the failure somewhere if I care about it.

Post reply on HN