6 ms·
Eschewing all hidden function calls is a reasonable design choice, but you give up a lot. No destructors means no smart pointers – and Zig doesn't seem to have
by comex 8y ago
Eschewing all hidden function calls is a reasonable design choice, but you give up a lot.
No destructors means no smart pointers – and Zig doesn't seem to have a garbage collector, so I guess you need to free() things manually.
No operator overloading means, among other things, no custom numeric-like types – including wrapper types for units (e.g. Feet) unless the language provides separate support for those (e.g. Go does, Zig seemingly doesn't).
- jetzzz 8y agoC++ and Rust need destructors because they both have unwinding and thus need to know how to destroy things. If there is no unwinding then calling destructors manually is not that bad if compiler checks that you do it. That said, I haven't used zig and don't know how it actually works.
- ioddly 8y agoI have yet to use Zig seriously, but I'm pretty sure "defer" is its solution to this problem: https://ziglang.org/documentation/master/#defer https://ziglang.org/documentation/master/#defer
- edflsafoiewq 8y agoI asked this in another thread, but how do generics work in the absence of destructors? Let's separate the idea of a destructor from the idea that a destructor is implicitly called at the end of a scope. In C++, if there weren't automatic destructor calls, you could still clean things up manually with a pseudo-destructor call, x.~T(), where T is the type of x. In C, there are no (user-defined) generics, so you always know the concrete type of x, so you know whether it needs to have a destructor called and what the name of that destructor is. In Zig, how would generic code create an instance of a generic type T that may or may not need to have a destructor called, ie. how do you know if you need to use defer and what the deferred expression should be? How would tooling be able to check that destructors were called if there is no special way of marking that a destructor exists?
- isaachier 8y agoHaving used Zig a bit, and C++ for years, I personally believe that this sort of scenario is pretty rare. You can always add a no-op dealloc method to your type if you want a generic function to handle it correctly. Moreover, Zig has awesome metaprogramming support so you can probably think of a way to check the type parameter for a dealloc method and only generate the dealloc call for those types.
- edflsafoiewq 8y agoPretty rare? What about ArrayList? I want ArrayList(T) to work whether T = i32 or T = ArrayList(i32). > You can always add a no-op dealloc method to your type if you want a generic function to handle it correctly You're looking at it from the perspective of the generic consumer, not the generic author. The generic author generally does not have the ability to edit the type. Plus there are many types that do not have methods at all (integers, points, etc.). > Moreover, Zig has awesome metaprogramming support so you can probably think of a way to check the type parameter for a dealloc method and only generate the dealloc call for those types. Yes, you can detect and call a method called deinit that has the appropriate signature using @reflect. You can even put this in a function and call it a pseudo-destructor. But there's no guarantee that deinit has the actual semantics of a destructor without some kind of language-level agreement between class authors.
- AndyKelley 8y ago> I want ArrayList(T) to work whether T = i32 or T = ArrayList(i32). This will work fine. Do you have a more specific example?
- edflsafoiewq 8y agoWhen I call deinit on an ArrayList(ArrayList(i32)) do the elements have deinit called on them?
- AndyKelley 8y ago
- comex 8y agoIf the compiler checks that you do it, sure. For local variables, I'd say `defer` is an okay but not great substitute. If you call malloc() yourself, then sure, it's not hard to remember to stick a "defer { free(ptr); }" afterwards. But what if you call a function that returns an owned value? Can you tell just from looking at it that you're meant to free the thing afterward? CoreFoundation, a C library on Apple platforms, has a cute solution to this: a strict naming convention. [1] If a function returns a value at "+1" reference count, i.e. you have to release it yourself, the function's name should include "Create" or "Copy", even where that would otherwise seem unnatural. For example, "CFSocketCopyAddress" returns a socket's address as a CFDataRef, returned at +1. You might expect it to be called "CFSocketGetAddress", but "Get" is reserved for functions that return values at +0. As I said, cute – but fundamentally a workaround for language limitations. Distinguishing between two kinds of functions is exactly the sort of menial bookkeeping that computers excel at, whereas humans are prone to making mistakes. And – while CoreFoundation is forever stuck with the goal of C compatibility, Objective-C, which used to rely on a very similar system of manual retain/release and naming conventions, did ultimately transition to automatic reference counting. But it doesn't have to be automatic. One use for destructors is to automatically release locks, and an interesting alternative design for that exists in Clang's "thread safety" annotations. These are annotations you can add to existing C or C++ code that uses mutexes; their main purpose is to catch you if you access a variable protected by a mutex without owning the mutex, but they can also complain if you lock something and forgot to unlock it. You can annotate functions as "requires mutex" (i.e. you must call it with the mutex locked, and it stays locked after the function), "acquire" (call with it unlocked, returns with it locked), "release" (the opposite), and so on. I think I once tried to abuse the annotation system to enforce reference counting, but it was broken. But I'd love to see a more thorough and robust design along these lines. [1] https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html#//apple_ref/doc/uid/20001148-103029 https://developer.apple.com/library/archive/documentation/Co...
- coldtea 8y ago>No operator overloading means, among other things, no custom numeric-like types – including wrapper types for units (e.g. Feet) unless the language provides separate support for those (e.g. Go does, Zig seemingly doesn't). Most languages survived without that.
- dagenix 8y agoPrior to their invention, most drivers survived without airbags in their cars too. comex just noted that the design decision here has some tradeoffs. One of those tradeoffs is that you can't define wrapper types for units. Such a feature has clear use cases - it's not hard to find instances where unit conversion bugs have had severe consequences. "Most languages survived without that" is not a constructive comment.
- dullgiulio 8y agoThat's not what parent said. You can express your maths as function calls. Operator overloading is often misused and violates the principle of minimal surprise. You could even have side effects. Still, type safety is totally possible. In Go, of your unit is Feet, you wrap your int into Feet and you cannot sum it with anything but feet. And that's all compile time, no cost.
- dagenix 8y agoRegardless of if comex's comment was fully accurate, I was mostly responding to "Most languages survived without that" which isn't productive.
- coldtea 8y agoIt's not just some historical curiosity from privative computing days. Huge successful current languages don't have operator overloading now either. If anything, outside numerical/scientific work, it's even frowned upon. And even in languages that do have them, it's not like people usually work with typed feet and such units. It is indeed safer, but I'm not sure it's mainstream (F#, Ada, Rebol, etc). Of course one can find such typed unit libs for other languages with Op. Overloading (C++ eg. has boost.units), but it's not like they're mainstream even where Operator Overloading is available. In any case, it's not the first argument to make against a system language that it doesn't support Op. Overloading as a means for this use case.
- nwmcsween 8y agoIt would be nice if a language offered resource aware functions like raml.co, then destructors could be enforced as O(1), etc.