Re: A const idiom + a different 'delete'

2010-08-12 Thread Pillsy
Shin Fujishiro Wrote: > bearophile wrote: > > This is an alternative way to write it that I've never used > < because I don't like it much: > > void main() { > > const(int[int]) aa = { > > int[int] result; > > foreach (i; 0 .. 10) > > result[i] = i * i; > >

Re: A const idiom + a different 'delete'

2010-08-12 Thread bearophile
Shin Fujishiro: > import std.conv; > > auto ref reverseArgs(alias fun, Args...)(auto ref Args args) > { > return mixin( > { > string rargs; > foreach (i, Arg; Args) > { > if (i > 0) > rargs ~= ", "; >

Re: A const idiom + a different 'delete'

2010-08-12 Thread Shin Fujishiro
bearophile wrote: > This is an alternative way to write it that I've never used because I don't > like it much: > > void main() { > const(int[int]) aa = { > int[int] result; > foreach (i; 0 .. 10) > result[i] = i * i; > return result; > }(); > } It lo

Re: A const idiom + a different 'delete'

2010-08-11 Thread Kagamin
bearophile Wrote: > void main() { > int[int] aa_; > foreach (i; 0 .. 10) > aa_[i] = i * i; > > // now I'd like to freeze aa > const(int[int]) aa = aa_; > } > void f() { { int[int] aa; //fill g(aa); } void g(ref const int[int] bb) { //do stuff }

Re: A const idiom + a different 'delete'

2010-08-11 Thread bearophile
Jonathan M Davis: > I think that's a total no-go because it would depend on program flow. You're > trying to alter a compile-time property at runtime. All it takes is having > delete within an if statement, and all of a sudden, depending on how your > program runs, the name may or may not be vis

Re: A const idiom + a different 'delete'

2010-08-11 Thread Adam Ruppe
Here's how I'd do it: void main() { const T[] buildArray() { T[] t; // fill in t return t; } const T[] t = buildArray; } So the creation is factored away into that nested function which is used to initialized the reference that you keep around.

Re: A const idiom + a different 'delete'

2010-08-11 Thread Jonathan M Davis
On Wednesday, August 11, 2010 10:52:30 bearophile wrote: > Jonathan M Davis: > > I think that's a total no-go because it would depend on program flow. > > You are of course right, it's the silly thing of the day. The #undef works > because it's not scoped. > > >You kind hide the variable creation

Re: A const idiom + a different 'delete'

2010-08-11 Thread bearophile
Jonathan M Davis: > I think that's a total no-go because it would depend on program flow. You are of course right, it's the silly thing of the day. The #undef works because it's not scoped. >You kind hide the variable creation inside another scope if you want this sort >of behavior:< You can'

Re: A const idiom + a different 'delete'

2010-08-11 Thread Andrej Mitrovic
p.s. Sorry about quoting large ammounts of text, I need to break out of that habbit. On Wed, Aug 11, 2010 at 7:28 PM, Andrej Mitrovic wrote: > The scope trick won't work. You can't modify a const after the declaration.

Re: A const idiom + a different 'delete'

2010-08-11 Thread Andrej Mitrovic
The scope trick won't work. You can't modify a const after the declaration. On Wed, Aug 11, 2010 at 7:26 PM, Jonathan M Davis wrote: > On Wednesday, August 11, 2010 08:59:29 bearophile wrote: > > This is an little idiom that is getting common in my code, it's similar > to > > the 'transients' of

Re: A const idiom + a different 'delete'

2010-08-11 Thread Jonathan M Davis
On Wednesday, August 11, 2010 08:59:29 bearophile wrote: > This is an little idiom that is getting common in my code, it's similar to > the 'transients' of Clojure language. > > Often I have to build a simple data structure like an array associative or > another kind of array, it needs to be mutab

Re: A const idiom + a different 'delete'

2010-08-11 Thread Andrej Mitrovic
Actually that was a pretty useless example. const needs to be evaluable at compile-time, so why did I bother creating a template function? lol.. On Wed, Aug 11, 2010 at 7:03 PM, Andrej Mitrovic wrote: > I think a more appropriate word for that would be "hide", or maybe > "freeze". > > Here's ano

Re: A const idiom + a different 'delete'

2010-08-11 Thread Andrej Mitrovic
I think a more appropriate word for that would be "hide", or maybe "freeze". Here's another alternative, if you can call it that :p : import std.stdio; auto freeze(alias dg)() { return cast(const) dg(); } void main() { auto aa = freeze!( { int[int] aa_; foreach (i; 0

Re: A const idiom + a different 'delete'

2010-08-11 Thread bearophile
> Here delete doesn't clean the aa_, it just removes the aa_ name from the > local namespace. It's similar to the #undef directive of C. Bye, bearophile

A const idiom + a different 'delete'

2010-08-11 Thread bearophile
This is an little idiom that is getting common in my code, it's similar to the 'transients' of Clojure language. Often I have to build a simple data structure like an array associative or another kind of array, it needs to be mutable because I fill it in few lines of code code. When it's comple