2 ms·
> - Pure functions: these allow you to statically guarantee that a piece of code doesn't perform any IO and is a completely deterministic function of its inputs
by piano 7y ago
> - Pure functions: these allow you to statically guarantee that a piece of code doesn't perform any IO and is a completely deterministic function of its inputs.
Is this actually true? As far as I know D's pure isn't the same as eg. Haskell pure, it only prevents modifications of global state, but you can still touch data referenced by arguments. I haven't done much D though so I'm not sure what the semantics really are...
- WalterBright 7y agoIf the parameters to a pure function are marked immutable, then you cannot change data referenced by those arguments. It's functionally pure. Ironically, sometimes people complain that the purity checks are too stringent :-) One thing interesting about pure functions in D is the purity restrictions can be relaxed by using a `debug` prefix: pure int test(int i) { printf("%d\n", i); // error, printf isn't pure debug printf("%d\n", i); // ok return i; } This makes it very, very handy to debug your functional code.