3 ms·
One of these days I wanted to try writing a C++ program in a totally immutable style, just to see how far you can take that concept. E.g. all classes have cons
by radarsat1 15y ago
One of these days I wanted to try writing a C++ program in a totally immutable style, just to see how far you can take that concept. E.g. all classes have const members. The only way to mutate the instance is to pass its members to a constructor for a new one. Using smart pointers this shouldn't cause too much memory-handling overhead. Function objects for higher-level functions. You'd almost get a functional OOP language. But it might be hard to stick with it. I'm not sure it would produce very fast code however, doing allocation and deallocation all the time. Maybe if it was combined with a smart allocator that could reuse discarded objects.
- eru 15y agoFunctional languages only get away with immutability, because they give you better tools for combining parts of your program. For example, higher order functions. I believe that you can write a totally immutable C++ program. But it's probably much harder than writing the same program in Haskell. You could try writing an immutable program in D. D is somewhat similar to C++, but much saner.