3 ms·
All it needs to be actually useful is destructors. And, constructors. I am always amazed when a new language omits destructors. Sure, CS assignments don't need
by ncmncm 5y ago
All it needs to be actually useful is destructors. And, constructors.
I am always amazed when a new language omits destructors. Sure, CS assignments don't need them, but out here we have real resources, not just memory, to manage.
- kristoff_it 5y agoThere's plenty of great languages with those features. Zig brings a different mindset to programming, so you need to empty your cup of tea before being able to enjoy Zig. https://ashidakim.com/zenkoans/1acupoftea.html https://ashidakim.com/zenkoans/1acupoftea.html
- jstimpfle 5y agoThe application I'm working on has about 8K lines of bare bones C + Win32 + some optional OpenGL currently. It probably has about 5 lines of repetitive cleanup code that can't be easily folded into a single place (so might benefit from RAII style cleanup). If I want to properly release even "static" resources so the app can be used as a library (which is not necessary), that number might grow to 20 lines. It's not something I'm sweating about, and I'm happy about all the time I saved by not doing "proper" RAII design prematurely, which has more ramifications and constraints than one might think.
- flohofwoe 5y agoMost of the problems that C++ added on top of C are caused by RAII though (e.g. a too rigid coupling of code and data). I think Zig made the right decision with the 'defer' keyword, even if it requires some change of perspective.
- deleted 5y ago[deleted]
- ncmncm 5y agoBy "problems" I guess you meant usefulness: "Most of the usefulness that C++ added on top of C is a product of RAII." Because usefulness is why C++ is used so much. And, "too rigid" meaning maximally flexible, likewise.
- flohofwoe 5y agoThat's what I mean with "change of perspective". POD structs are entirely fine if a library API is designed from the ground up for them. It only becomes a problem when trying to write C++ code in C (or Zig in this case).
- ncmncm 5y agoPOD structs are fine if your wants are limited. Most usually, they are more error-prone and hard to use, and often slower. Limiting yourself to PODs is, first and foremost, limiting yourself. There are reasons why, in the highest-paid programming jobs, you would be laughed from the room if you suggested coding something in C. Dull knives, cracked pots, and filthy ingredients make a bad stew.
- masklinn 5y agoDestructors are good. Though if the language does not have unwinding, catchable panics you could be even better served with linear types. Constructors, though, are solely an attractive nuisance. For the constructors themselves you're better off with factory function whose semantics match your needs (fallibility, or the lack thereof, being a big one) and for other protocols (e.g. copy, move, conversion, ...) you're better off either not having them or having dedicated protocols which are clearly and explicitly opt-in, depending on language semantics. > I am always amazed when a new language omits destructors. Destructors require deterministic destruction, so that only works for non-managed languages. That's why managed languages try to find alternative solutions to the resource problem (except for languages used to research substructural typing I guess, as well as languages which mandate refcounting as part of their semantics).