July 17, 2022

On Sunday, 17 July 2022 at 16:33:29 UTC, Nick Treleaven wrote:

>
auto sa = $[1,2,3,4];

That avoids supporting partial auto declarations (a reason why Walter didn't like that PR). It also allows inference when passing a literal to a template function argument where the parameter is not already a static array. The $[] syntax would literally mean "length array", i.e. fixed length array. (Why do we overload the word 'static' here for arrays?)

Hmm, it's not half bad!

Another idea... to enumerate all possibilities.

scope sa = [1,2,3,4];
July 17, 2022

On Sunday, 17 July 2022 at 16:33:29 UTC, Nick Treleaven wrote:

>
auto sa = $[1,2,3,4];

Maybe choose something that is consistent with other literals, the type is generally a suffix:

"hello"c // string
"hello"w // wstring
"hello"d // dstring
12345UL

July 17, 2022

On Sunday, 17 July 2022 at 16:33:29 UTC, Nick Treleaven wrote:

>

On Sunday, 17 July 2022 at 15:23:45 UTC, Steven Schveighoffer wrote:

>

The second statement is much better, but you need the first to infer the length. Just specifying "infer the length" with a [$] on the type is a perfect fit.

There may be ambiguity with $ for length inside an index/slice expression when an inferred static array type is part of an expression:
https://forum.dlang.org/post/qsjahatdiyxwojcwyedm@forum.dlang.org

It would not be ambiguous, as int[$] would refer to the new type.

I'm having a hard time believing that kind of expression is currently used anywhere.

it would be consistent with using $ inside an expression of 2 nested arrays:

int[] arr;
int[] foo;

return arr[0 .. foo[$-1]]; // does $ refer to arr.length or foo.length?
> >

You also have an issue with const conversion that's not as easy to specify. e.g.:

char[74] foo = "this is a long string that I don't want to have to count the characters in";

Presumably length inference would not include the terminating nul byte of the string literal?

Of course, that's not actually included in the literal length. e.g. "".length == 0.

> >

Can't use staticArray here because the type will be immutable(char)[74].

If I want to specify it should be char, I can't do staticArray!char, because then it doesn't infer anything.

Could add an overload for that:
https://forum.dlang.org/post/urzdeerfcvardyztxfab@forum.dlang.org

Fair point (if anything could someone create an issue on this so it's not forgotten?). But I still think the nested syntax isn't good.

> >

This is a simple quality-of-life improvement. I think the original PR was reverted because it allowed too much inference, like:

auto[$] foo = [1,2,3,4];

I think just specifying the length is dependent on the length of the literal is enough benefit to justify the change. We don't need full inference of any static array type.

Threads like these always use basic element types like int or char. In practice the element type can be quite complex. It's a pain to have to write out a complex type - e.g. typeof(x.map!(a => someExpr).front).

The use case for this escapes me. You can't do this with staticArray anyway.

auto x = arr.map!(a => a * 2).array.staticArray; // error, can't pass Something[] to staticArray

So current code that might build such a thing isn't any worse off with the feature.

What you need to write for this use case is a complex voldemort-type-producing expression, and then make multiple calls to that thing, inside an array expression. I just don't see it being a thing.

>

That could be solved by having specific syntax for static array literals:

auto sa = $[1,2,3,4];

Maybe, maybe not. Yes, without a special syntax on the right side, you'd need a type for it, but I don't see a really convincing use case for this.

That being said, I would be ok with this syntax, or something that's identified on the expression. Hell, even if it was __staticArray(expr), that would be at least a possible solution.

>

That avoids supporting partial auto declarations (a reason why Walter didn't like that PR).

That PR discussion is... interesting. I don't know exactly what the rejection was, aside from "this will just release the floodgates of crappy feature requests".

>

It also allows inference when passing a literal to a template function argument where the parameter is not already a static array. The $[] syntax would literally mean "length array", i.e. fixed length array.

I'm not opposed to it exactly. But it does suffer the same problems as staticArray. It is sometimes nice to use the left-hand-side of the construction/assignment to determine how the literal is interpreted.

>

(Why do we overload the word 'static' here for arrays?)

I hate that they are called "static" arrays also. fixedLengthArray is probably not a great function name though.

>

And it's prefix syntax, which would read well with your nested static array example:

auto foo = $[$[1,2,3,4],[5,6,7,8]];

It doesn't help your char[$] foo = "immutable data"; case though (which would need the staticArray overload or equivalent).

Oh yeah, agreed. Maybe something can be done about that in the syntax, like $char[...] I don't know.

-Steve

July 18, 2022

On Sunday, 17 July 2022 at 16:33:29 UTC, Nick Treleaven wrote:

>

literally mean "length array", i.e. fixed length array. (Why do we overload the word 'static' here for arrays?)

It's fairly common terminology. I couldn't tell you when it first came into usage, but it's used to describe fixed-length arrays in multiple languages.

July 18, 2022

On Monday, 18 July 2022 at 01:25:38 UTC, Mike Parker wrote:

>

On Sunday, 17 July 2022 at 16:33:29 UTC, Nick Treleaven wrote:

>

literally mean "length array", i.e. fixed length array. (Why do we overload the word 'static' here for arrays?)

It's fairly common terminology. I couldn't tell you when it first came into usage, but it's used to describe fixed-length arrays in multiple languages.

And I should add, I don't see it as being an overloaded usage of the term. Static generally means "fixed" in one form or another, in the sense that the properties of whatever it's applied to are determined prior to run time.

July 18, 2022

On Thursday, 14 July 2022 at 13:14:59 UTC, ryuukk_ wrote:

>

It is misleading, nobody expect this to be GC allocated

It should be equal to:

int[3] arr = [1, 2, 3]

Also why it is GC allocated without requiring new?

DMD 2.099 has introduced crippling bugs that arise from overzealous stack allocation. If I could have a command line switch that would always heap allocate everything not completely obviously a stack allocation, ie. always heap allocate closures, always heap allocate arrays, I would put it in our build scripts and never touch it again.

Stack allocation without extremely reliable escape analysis is an express ride to corruptiontown. And D does not have extremely reliable escape analysis.

July 18, 2022

On Monday, 18 July 2022 at 01:29:11 UTC, Mike Parker wrote:

> >

It's fairly common terminology. I couldn't tell you when it first came into usage, but it's used to describe fixed-length arrays in multiple languages.

And I should add, I don't see it as being an overloaded usage of the term. Static generally means "fixed" in one form or another, in the sense that the properties of whatever it's applied to are determined prior to run time.

Yes, it's not something D invented, static vs dynamic array. But we have so many other uses of the static keyword. In particular, static int[] x;, which is an array but not a static array! Fixed length array is clearer.

July 18, 2022

On Sunday, 17 July 2022 at 17:23:18 UTC, Daniel N wrote:

>

Another idea... to enumerate all possibilities.

scope sa = [1,2,3,4];

That is fine to stack-allocate dynamic array elements, but I think making that mean a fixed-length array would cause problems with typing. E.g. inferring scope would change the type of sa and fixed-length arrays are not ranges, etc.

July 18, 2022

On Monday, 18 July 2022 at 12:13:47 UTC, FeepingCreature wrote:

>

And D does not have extremely reliable escape analysis.

Can you please post an example of @safe -dip1000 code that allows memory corruption? (There is a known problem with constructors with a PR to fix).

Also I presume the bugs you're talking about aren't just due to the in preview switch?

July 18, 2022

On Sunday, 17 July 2022 at 20:07:37 UTC, Steven Schveighoffer wrote:

>

On Sunday, 17 July 2022 at 16:33:29 UTC, Nick Treleaven wrote:

>

There may be ambiguity with $ for length inside an index/slice expression when an inferred static array type is part of an expression:
https://forum.dlang.org/post/qsjahatdiyxwojcwyedm@forum.dlang.org

It would not be ambiguous, as int[$] would refer to the new type.

OK, hopefully it is straightforward to implement that.

>

I'm having a hard time believing that kind of expression is currently used anywhere.

Probably not common, theoretically possible. The linked example doesn't compile. I expect it's OK to change.

> >

Presumably length inference would not include the terminating nul byte of the string literal?

Of course, that's not actually included in the literal length. e.g. "".length == 0.

OK, I think that's the correct choice. It does mean arr.ptr isn't nul-terminated to pass to C, using length inference. That would mean people need to be careful doing that with a string literal and manually write the \0. Currently you have to think about the size of the array.

...

>

The use case for this escapes me. You can't do this with staticArray anyway.

auto x = arr.map!(a => a * 2).array.staticArray; // error, can't pass Something[] to staticArray

Yes, bad example. There may be meta-programming related cases though, e.g.:

CommonType!(mixin(someLongExpr))[$] arr = [mixin(someLongExpr)];

auto + fixed-length array literal would be better there. Probably not common though.

...

> >

It also allows inference when passing a literal to a template function argument where the parameter is not already a static array. The $[] syntax would literally mean "length array", i.e. fixed length array.

I'm not opposed to it exactly. But it does suffer the same problems as staticArray.

Yes, but it doesn't need an import and is concise.

>

It is sometimes nice to use the left-hand-side of the construction/assignment to determine how the literal is interpreted.

Yes. And if a literal is needed, one could use cast(T[$]) (assuming T is simple).