Eliminate assert and lazy from D?

Michel Fortin michel.fortin at michelf.com
Tue Oct 13 05:11:03 PDT 2009


On 2009-10-13 00:33:57 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> Right now, the language has enough power to express assert as a library 
> function, as opposed to a primitive construct. (See e.g. enforce.) I 
> think it would be good to relegate assert to object.d.

According to the spec, 'assert' also allows the compiler to know 
something will always be true, which could result in optimizations. The 
compiler also enforce that you have an assert(false) at the end of a 
function when the control flow can't be proven to never reach it. So 
no, 'assert' should stay in the language.


> This also brings up "lazy", which seems to be quite botched. Are there 
> suggestions on how to replicate its functionality in a different way?

There are two uses for 'lazy':

1. provide a statement that can be executed by the function you're 
calling to perform some action.
2. to give a lazy-evaluated value to the calling function, removing the 
cost of evaluation when unnecessary

I'm willing use the delegate literal syntax for case 1. But I'd like to 
keep 'lazy' for case 2. The problem with case 2 is that it's more an 
optimization technique used within the function (lazy evaluation) and 
it would be better if the caller didn't have to care about such details.

So here's my idea to fix 'lazy': make sure 'lazy' can only work with 
case 2 by forcing the lazy expression to be pure, thus with no side 
effects. This way, 'lazy' becomes an almost-transparent optimization 
technique and stays true to its meaning of 'lazy evaluation'. And it'll 
give more optimization opportunities to the compiler.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list