4 ms·
D has a `pure` attribute for functions, and yes it is enforced. It's enforced even to the point where it becomes a PITA, but when you manage to make functions p
by WalterBright 7y ago
D has a `pure` attribute for functions, and yes it is enforced. It's enforced even to the point where it becomes a PITA, but when you manage to make functions pure, you know it's solid.
One pain point with pure functions is you can't insert debug logging statements. D has a special case for that - purity for a statement isn't checked if it is prefixed with the `debug` keyword:
pure int square(int x) {
debug printf("called square\n");
return x * x;
}
which is very, very handy.
- Ididntdothis 7y agoThis seems a very pragmatic solution.
- SomeOldThrow 7y agoThis means you can't re-order the computation, though, or memoize it—at least not the the extent it's possible without IO.
- mhh__ 7y agoThe use of the debug {} block in D is opt-in e.g. the compiler only includes those statements when given the debug flag https://d.godbolt.org/z/Rwu4Cq https://d.godbolt.org/z/Rwu4Cq (remove "-d-debug" to see)
- Gibbon1 7y agoI wonder does that mean you could also enforce freeing memory allocated by a pure function as well?
- mhh__ 7y agoI'm not sure what you mean? Allocators are often pure in D, they probably shouldn't be but that would mean allocating outside the function which would be ugly and probably buggy too
- Gibbon1 7y agoObviously a pure function can allocate memory on the stack. I would think it could also allocate memory on the heap, iff it was guaranteed that the memory was freed before the function exits. Was thinking that would be something the compiler would do. Forgive me I'm just a small brained firmware programmer.
- ricattierger 7y agoSo it seems that you are sort of talking about a function that is memoizable but not pure. Pure has no mutation of any global state. So hypothetically touching the heap means not pure - if somehow others can see it.
- boomlinde 7y agoSo where do we draw the line? Of course, no real computer implementation of any language is going to be able to generate fundamentally pure code because in the end they're going to be mutating memory (at the very least the program counter). That's not a very useful distinction, and usually one talks about "purity" at a conceptual level. The language provides a layer of abstraction in which things can be considered pure (or not), even if the underlying implementation e.g. makes optimizations that require mutation. The language is just a guarantee that you can ignore these aspects of its implementation.
- ricattierger 7y agoI think that is a good point at the end. Pure may be defined in regards to the language. So allocating memory in C may make a function unpure but something other languages may still be pure.
- Gibbon1 7y agoRolling it over I think vaguely along the idea that marking a function as pure is a contract that tells the compiler that the function and it's call tree ultimately shouldn't modify external state. A function that allocates memory isn't pure, except perhaps iff it frees that memory either itself or by compiler magic. Then perhaps it is.
- Y_Y 7y agoThe same "trick" is especially handy in a pure-by-design language like Haskell. There you have the `trace` function which prints to stdout while pretending not to. It's similarly useful, even if it may get called zero or many times. Is the same true of D's debug?
- WalterBright 7y agoNo, in D any statement prefixed with `debug` does not get its purity checked. `debug` prefixed code is enabled via a command line switch, otherwise it is excised. I.e. it's for conditionally inserting debugging code where you don't care about purity, you just want to find the bug.
- einpoklum 7y agoIsn't that erroneous, in the sense that printf() has side-effects, breaking the "pure" promise?
- WalterBright 7y agoIndeed it does break the pure promise. But it's only for debugging builds.
- Doxin 7y agoFurthermore AFAIK things prefixed with debug don't get compiled into release binaries either.