The scope trick won't work. You can't modify a const after the declaration.<br><br><div class="gmail_quote">On Wed, Aug 11, 2010 at 7:26 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisprog@gmail.com">jmdavisprog@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">On Wednesday, August 11, 2010 08:59:29 bearophile wrote:<br>
> This is an little idiom that is getting common in my code, it's similar to<br>
> the 'transients' of Clojure language.<br>
><br>
> Often I have to build a simple data structure like an array associative or<br>
> another kind of array, it needs to be mutable because I fill it in few<br>
> lines of code code. When it's complete I never need to change it again, so<br>
> now I'd like it to be const. I can't freeze it, so I have to create a new<br>
> variable:<br>
><br>
><br>
> void main() {<br>
> int[int] aa_;<br>
> foreach (i; 0 .. 10)<br>
> aa_[i] = i * i;<br>
><br>
> // now I'd like to freeze aa<br>
> const(int[int]) aa = aa_;<br>
> }<br>
><br>
><br>
> If the array is fixed-sized the situation is worse because the last line<br>
> copies the whole array.<br>
><br>
> Sometimes I use a small function to do this.<br>
><br>
> This is an alternative way to write it that I've never used because I don't<br>
> like it much:<br>
><br>
> void main() {<br>
> const(int[int]) aa = {<br>
> int[int] result;<br>
> foreach (i; 0 .. 10)<br>
> result[i] = i * i;<br>
> return result;<br>
> }();<br>
> }<br>
><br>
><br>
> In Python I sometimes use "delete" to remove a name from the local<br>
> namespace that I don't want to use any more. This cleaning purpose may be<br>
> a replacement purpose for the delete keyword, but I don't know if you like<br>
> it:<br>
><br>
><br>
> void main() {<br>
> int[int] aa_;<br>
> foreach (i; 0 .. 10)<br>
> aa_[i] = i * i;<br>
><br>
> // now I'd like to freeze aa<br>
> const(int[int]) aa = aa_;<br>
> delete aa_; // from now on the 'aa_' name can't be seen/used.<br>
> }<br>
><br>
><br>
> Here delete doesn't clean the aa_, it just removes the aa_ name from the<br>
> local namespace. If there's another reference to aa_ it is unchanged and<br>
> it sees an unchanged associative array. The point of removing the aa_ name<br>
> is to keep the namespace tidy.<br>
><br>
> As with variable definitions a goto can't jump over one delete. In Python<br>
> you can later create a new variable with the same name, but in D the<br>
> variable doesn't actually vanish from the stack, so I think it's better to<br>
> disallow successive redefinitions of it.<br>
><br>
> Bye,<br>
> bearophile<br>
<br>
</div></div>I think that's a total no-go because it would depend on program flow. You're<br>
trying to alter a compile-time property at runtime. All it takes is having<br>
delete within an if statement, and all of a sudden, depending on how your<br>
program runs, the name may or may not be visible. You kind hide the variable<br>
creation inside another scope if you want this sort of behavior:<br>
<div class="im"><br>
void main()<br>
{<br>
const(int[int]) aa;<br>
<br>
{<br>
</div><div class="im"> int[int] aa_;<br>
foreach(i; 0 .. 10)<br>
aa_[i] = i * i;<br>
</div> aa = aa_;<br>
}<br>
}<br>
<br>
<br>
That naturally removes the name from the namespace just fine. It's possible that<br>
this particular idiom calls for some sort of language addition or having<br>
something added to Phobos, but removing symbols from a namespace with statements<br>
is not going to work very well - if at all - in a compiled, statically-typed<br>
language like D.<br>
<font color="#888888"><br>
- Jonathan M Davis<br>
</font></blockquote></div><br>