"inline" conversion of array to immutable
Jeff Thompson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 22 04:46:40 PDT 2016
On Friday, 22 April 2016 at 11:16:59 UTC, ag0aep6g wrote:
> It's a nested function now. That means, it could reference
> local variables of main. Make func static and it works.
>
> You can also use a function literal:
> ----
> void main()
> {
> immutable int[] array = {
> int[] result = new int[10];
> result[0] = 1;
> return result;
> }();
> }
Great! Making it static works. The function literal also works if
I add "function int[]()":
void main(string[] args) {
immutable int[] array = function int[]() {
int[] result = new int[10];
result[0] = 1;
return result;
}();
}
More information about the Digitalmars-d-learn
mailing list