== Quote from bearophile (bearophileh...@lycos.com)'s article > foobar: > > you raise a valid concern but this looks too complicated. > > I'd suggest to simplify into only two cases. > > > > // 1) T.INIT - as you suggested the dimension should be checked > > auto foo = new int[][](10, 20); // correct > > auto foo1 = new int[][](10); // compilation error > Keep in mind that currently this is correct and it generates a 1D dynamic > array: > auto v = new int[10];
Isn't this an inconsistency in the language? // Generally speaking, allocates an instance of T on the heap auto foo = new T; However, "int[10]" and "new int[10]" are different types. > > // 2) function of the array dimension > > auto bar = new int[][](10, 20, (int x, int y) { return x*y; } ); > > > > // if you want a default value just use: > > auto bar1 = new int[][](10, 20, { return 3; } ); > > > > For fixed-sized arrays the function would be CTFE-able. > This is more complex than my suggestions :-) > The most common case is the initialization with a constant value. I'd like > this case to be as efficient as possible, so I don't like the { return 3; }. This syntax could be simplified if we drop "return" as suggested in another thread. Also, couldn't we employ "lazy" for this? I don't remember how it works, does it accept a value on the call-site? On the other hand, I dislike the named-parameter suggestion. Named parameters indicate in my eyes a design bug. > The case with a more complex delegate is interesting to initialize constant > arrays: > immutable m = new int[][](10, 20, (int x, int y){ return x*y; }); > But in my opinion this is not a so common operation. And it's not able to replace Python-style array comps: > foo = [x ** x for x in lazyIterable] > So I don't like your proposals. > Maybe dsimcha will give a Phobos function to allocate & initialize a nD array with a given const value. > Bye, > bearophile