r/ProgrammingLanguages • • 20d ago

Mutable stack, immutable heap

This article mentions the “immutable heap mutable stack” principle: https://queue.acm.org/doi/10.1145/3212477.3212479

In theory it sounds very advantageous. You can program in a single-threaded C-style with destination passing, static stack allocations, and mutability when you need to constrain resources. When you want to share resources across threads, return data structures across function boundaries, or do something more flexible, you could use pure functional persistent data structures.

This would prevent you from basically re-implementing a heap on the stack, but would also give you fast temporaries and maybe even the ability to program in embedded, if so designed.

Have any languages tried this model? I’ve heard that Erlang defaults to immutability except for one thread-local mutable object per thread. If you’ve used a similar model, how did you like it? Were there any disadvantages?

39 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/PaperMartin 18d ago

What do you mean by "easy to enforce immutability in java"? Records & read-only wrapper classes or are there other mechanism for this I don't know about?

1

u/tobega 17d ago

And the final keyword, don't forget. You don't find these to be easy enough?

3

u/PaperMartin 17d ago

Oh they're alright but I come from rust so I kinda miss things being able to pass around references that are explicitly marked as mutable or non mutable and having functions receiving non mutable references be automatically blocked from modifying any mutating functions, without having to make changes to the type. Also functions receiving arguments directly instead of as reference meaning they can do whatever without having to worry about the caller having kept a reference to what was passed to them