Purity in Java & D

Jonathan M Davis jmdavisprog at gmail.com
Sun Sep 5 18:17:29 PDT 2010


On Sunday 05 September 2010 16:59:19 bearophile wrote:
> Simen kjaeraas:
> > So it is basically exactly like D, only no mutable global state?
> 
> It's similar, of couse. I think there is no 'pure' tag, the compiler infers
> if a method is pure on the base of its arguments. In D even a function
> that is technically pure is not seen as pure of you don't tag it with
> 'pure'.
> 
> Bye,
> bearophile

Having the compiler determine purity would be cool, but it runs into a few of 
problems.

Like any recursion, you need a base case to end it at. With purity, you'd need a 
base set of operations which were determined to be pure by the language itself. 
This should be feasible, but I'm not sure how many changes would be necessary. 
It's probably already done, since the compiler already has to scream at you if a 
function marked pure isn't truly pure, but there might be some extra stuff 
needed.

Because the compiler must start at that base set of functions, it would have to 
mark everything pure that it can, and then loop through all functions that 
aren't yet pure and mark them pure if it can until it reaches an iteration where 
it can no longer mark anything as pure. This has 2 serious flaws:

1. This would likely be _expensive_ in terms of compile-time. I don't _think_ 
that it runs into the halting problem, but it might if some fancier language 
features come into play.

2. In the current compilation model, each function is compiled separately. As I 
understand it, in D (unlike C or C++), they could even theoretically be compiled 
_simultaneously_. By trying to determine the purity of a function by looking at 
every other function that it calls, you've created a whole lot of dependencies, 
and that totally breaks the compilation model.

So, I really don't know how those folks could reasonably get away with not 
marking stuff as pure. I can see why it would be desirable to have the compiler 
figure it out (there are all kinds of cool optimizations that could be done if we 
ditched the separate compilation model), but it doesn't seem to be a reasonable 
thing to do.

- Jonathan M Davis


More information about the Digitalmars-d mailing list