auto arr = [1, 2, 3] should be a static array, not a GC allocated array
ryuukk_
ryuukk.dev at gmail.com
Tue Jul 19 15:27:57 UTC 2022
On Tuesday, 19 July 2022 at 14:04:09 UTC, Quirin Schroll wrote:
> 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:
>>
>> ```D
>> int[3] arr = [1, 2, 3]
>> ```
>>
>> Also why it is GC allocated without requiring ``new``?
>
> Because it’s useful to more people than if it were the
> opposite. If you find that `staticArray` too verbose, there’s a
> single-keystroke way to get pseudo-literals for static arrays:
>
> ```D
> struct s
> {
> static typeof([ Ts.init ][0])[Ts.length]
> opIndex(Ts...)(auto ref Ts args)
> {
> import std.functional : forward;
> return [ forward!args ];
> }
> }
> ```
> Usage:
> ```D
> void main()
> {
> auto empty = s[];
> static assert(is(typeof(empty) == void[0]));
>
> auto xs = s[1,cast(byte)2,3];
> static assert(is(typeof(xs) == int[3]));
>
> static struct NoCopy { @disable this(this); }
> auto noCopies = s[NoCopy(), NoCopy()];
> }
> ```
> You’re free to choose any name (except keywords). If you name
> it `stackalloc`, it almost looks like taken from C#.
That's not the point..
The difference between:
- having to write the helper code
- having to tap into the std
- having then to import this helper module everywhere
- having to spend time doing all of the above and carry that file
in all my projects
vs
Having to just type `stackalloc` in the C# example, or `_` in
some other languages
For something as basic as wanting an inferred fixed static array
length
So the language can help me so i don't have to manually count how
many elements are in there: `auto arr =
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];`
Let's stick with C then, why bother coming up with a better
language?
Same with Tagged Union, same with Tuple, same with Nullable
And then it always end up in these endless discussion where one
has to constantly remind people this simple fact, and then he
feels demotivated, and then withdraw his suggestion, and then
gets interested in an other language instead
I'm not a compiler/language developer, i am a language user,
little things like that makes the difference
More information about the Digitalmars-d
mailing list