Transients or scoped immutability
bearophile
bearophileHUGS at lycos.com
Tue Apr 26 04:43:05 PDT 2011
Don:
> Just use a delegate literal (with a fix to 5081).
So I think you are suggesting a solution to my original problem similar to:
void foo(char[] data) {
const count_bar = pure {
int[256] count;
foreach (char c; data)
count[c]++;
int x = ...
auto bar = map!(...)(...x...);
return tuple(count, bar);
}();
const count = count_bar[0];
const bar = count_bar[1];
// Here x is not visible.
// Here count and bar are visible but read-only.
}
void main() {
foo("this is a string");
}
This is less nice looking than possible alternatives, but it's not terrible, expecially if the tuple unpacking syntax sugar gets added to D:
(How do you create pure delegates? A syntax like this doesn't work: const x= pure {return 0;})
void foo(char[] data) {
const (count, bar) = pure {
int[256] count;
foreach (char c; data)
count[c]++;
int x = ...
auto bar = map!(...)(...x...);
return tuple(count, bar);
}();
// Here x is not visible.
// Here count and bar are visible but read-only.
}
void main() {
foo("this is a string");
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list