logoalt Hacker News

fwlr • today at 4:29 PM • 2 replies • view on HN

Oh, wow, this is a fantastic explanation for why “Zig makes you pass the allocator around as an argument to all your functions” is A Good Thing, Actually. Kudos to the author!


Replies

mac3n • today at 7:09 PM

This argues for dynamically scoped variables - bindings that are scoped and inherited by all called functions without having to be passed explicitly. After all, why should everyone in the call chain need to know if some deep call allocates temporary storage.

It's always bothered me that the only such feature in many languages is exception handlers (I'm also bothereed by functions that throw things).

I want "late" bindings, implemented as "shallow" bindings. "Late" as in binding is established at runtime in the call chain. "Shallow" as in you don't need to crawl the call chain to find it. Something like

    extern Allocater *theAllocater = &DefaultAllocater;
    ...
    // allocater scope
    { Allocater *prev = theAllocater;
      theAllocater = NewAllocater();
      ...
      theAllocater->free();
      theAllocater = prev;
    }
with a deep call using the current allocater

    // deep call
    { tmp = theAllocater->alloc(size);
      ...
    }
➕ show 1 reply
pdlclay • today at 5:03 PM

[flagged]