D const design rationale

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Jun 23 06:05:53 PDT 2007


Martin Howe wrote:
> "Reiner Pope" <some at address.com> wrote in message 
> news:f5ipqd$2m6j$1 at digitalmars.com...
> 
>> can we expect some annotation for functions which says 'this function 
>> doesn't read/write any global variables?'
> 
> Personally, I like the PHP way; each global variable must be declared as 
> such before use; the syntax is similar to a local variable. Any function 
> including any "global" declarations is thus "not pure".

That might actually work in D: in PHP, it was, for me, the source of countless
bugs, as you don't have to declare local variables before use. Thus I'd happily
use something like "return $b++" without "global $b" and wonder why the function
always returned zero.

A good plus about such a requirement is that it discourages you from writing
functions that rely on many globals: when you start thinking about something
like http://www.php.net/manual/en/language.variables.scope.php#18748 you should
consider refactoring instead.

However, it can still make code unwieldy when you're writing a lot of
one-liners, each of which need to refer to the same global, and you need to
repeat the "global foo bar" in each one.

I'd say "pure" wins here, though "global" isn't without its merits.

-- 
Remove ".doesnotlike.spam" from the mail address.



More information about the Digitalmars-d mailing list