Jump to page: 1 2 3
Thread overview
Inferring static array size
Apr 28, 2024
NotYouAgain
Apr 28, 2024
Chloé
Apr 28, 2024
NotYouAgain
Apr 28, 2024
Chloé
Apr 28, 2024
NotYouAgain
Apr 28, 2024
Mike Parker
Apr 28, 2024
ryuukk_
Apr 28, 2024
NotYouAgain
Apr 30, 2024
electricface
Apr 30, 2024
NotYouAgain
May 03, 2024
rkompass
May 03, 2024
NotYouAgain
May 03, 2024
rkompass
May 03, 2024
Nick Treleaven
May 03, 2024
NotYouAgain
May 03, 2024
NotYouAgain
May 03, 2024
rkompass
May 04, 2024
NotYouAgain
May 03, 2024
Nick Treleaven
May 04, 2024
rkompass
May 04, 2024
Nick Treleaven
May 03, 2024
Nick Treleaven
May 03, 2024
rkompass
May 04, 2024
NotYouAgain
May 04, 2024
Stefan Koch
May 16, 2024
Quirin Schroll
April 28, 2024
The idea is simple, really, really simple.

Instead of this:

 int[5] a = [1, 2, 3, 4, 5];

allow this:

 int[$_] a = [1, 2, 3, 4, 5];

Both are static arrays.

Having to change the length everytime i change the initialisation list is a pain in the $____

The compiler infers the size in the second example, based on the number of elements in the initialisation list. Just like C can do.

April 28, 2024
On 4/28/24 11:31, NotYouAgain wrote:
> The idea is simple, really, really simple.
> 
> Instead of this:
> 
>   int[5] a = [1, 2, 3, 4, 5];
> 
> allow this:
> 
>   int[$_] a = [1, 2, 3, 4, 5];
> 
> Both are static arrays.
> 
> Having to change the length everytime i change the initialisation list is a pain in the $____
> 
> The compiler infers the size in the second example, based on the number of elements in the initialisation list. Just like C can do.
> 

The Phobos function staticArray can be used to infer the array length while explicitly specifying (or inferring) the element type:

    import std.array : staticArray;
    auto a = [1, 2, 3, 4, 5].staticArray;  // inferred int[5]
    auto b = [1, 2, 3, 4, 5].staticArray!float;  // inferred float[5]

April 28, 2024
On Sunday, 28 April 2024 at 09:35:22 UTC, Chloé wrote:
>
> The Phobos function staticArray can be used to infer the array length while explicitly specifying (or inferring) the element type:
>
>     import std.array : staticArray;
>     auto a = [1, 2, 3, 4, 5].staticArray;  // inferred int[5]
>     auto b = [1, 2, 3, 4, 5].staticArray!float;  // inferred float[5]

Oh. interesting, I didn't know. Thanks.

but...

auto a = 20.iota.array.staticArray;
    //  Error: none of the overloads of template `std.array.staticArray` are callable using argument types `!()(int[])`


April 28, 2024
On Sunday, 28 April 2024 at 09:31:01 UTC, NotYouAgain wrote:
> The idea is simple, really, really simple.
>
> Instead of this:
>
>  int[5] a = [1, 2, 3, 4, 5];
>
> allow this:
>
>  int[$_] a = [1, 2, 3, 4, 5];
>
> Both are static arrays.
>
> Having to change the length everytime i change the initialisation list is a pain in the $____
>
> The compiler infers the size in the second example, based on the number of elements in the initialisation list. Just like C can do.

FYI, a DIP for this was submitted and withdrawn by the author in response to negative feedback. See below, and particularly the "Reviews" section at the bottom of the document.

https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1039.md
April 28, 2024
On 4/28/24 11:43, NotYouAgain wrote:
> On Sunday, 28 April 2024 at 09:35:22 UTC, Chloé wrote:
>>
>> The Phobos function staticArray can be used to infer the array length while explicitly specifying (or inferring) the element type:
>>
>>     import std.array : staticArray;
>>     auto a = [1, 2, 3, 4, 5].staticArray;  // inferred int[5]
>>     auto b = [1, 2, 3, 4, 5].staticArray!float;  // inferred float[5]
> 
> Oh. interesting, I didn't know. Thanks.
> 
> but...
> 
> auto a = 20.iota.array.staticArray;
>      //  Error: none of the overloads of template `std.array.staticArray` are callable using argument types `!()(int[])`
> 
> 

To CTFE a range into a static array, try:

    staticArray!(20.iota)

Note the exclamation mark. This uses the following overload:

    auto staticArray(alias a)()
        if (isInputRange!(typeof(a)))

April 28, 2024
On Sunday, 28 April 2024 at 09:50:05 UTC, Chloé wrote:
>
> To CTFE a range into a static array, try:
>
>     staticArray!(20.iota)
>
> Note the exclamation mark. This uses the following overload:
>
>     auto staticArray(alias a)()
>         if (isInputRange!(typeof(a)))

Yes that works. Thanks.

Not as intuitive as [$_] would be, but still a usable solution.

At least I don't have to put my array in a separate module for it to work ;-)
April 28, 2024

On Sunday, 28 April 2024 at 09:31:01 UTC, NotYouAgain wrote:

>

The idea is simple, really, really simple.

Instead of this:

int[5] a = [1, 2, 3, 4, 5];

allow this:

int[$_] a = [1, 2, 3, 4, 5];

Both are static arrays.

Having to change the length everytime i change the initialisation list is a pain in the $____

The compiler infers the size in the second example, based on the number of elements in the initialisation list. Just like C can do.

YES please, this is long overdue, there is no reason to not have this

However, i don't like the proposed syntax, it should be 1 character to type, and should not be a ugly one

_ should be enough

April 28, 2024
On Sunday, 28 April 2024 at 10:17:06 UTC, ryuukk_ wrote:
>
> YES please, this is long overdue, there is no reason to not have this
>
> However, i don't like the proposed syntax, it should be 1 character to type, and should not be a ugly one
>
> `_` should be enough

So my thinking behind [$_] is:

'$' is already a synonym for the length of the array.

e.g
 int[2] arr = [1,2];
 writeln(arr[0..arr.length]); // [1, 2]
 writeln(arr[0..$]); // [1, 2]

so with [$_] ... '$_' means 'array length' and '_' means.. please compiler fill in missing blank.

true, [_] can just as easily mean .. please compiler fill in the missing blank.

Frankly I don't mind either.

[_] is easier to type, but not as easy on the eye I think ;-)  ..looks kinda like a box in my ide.

April 29, 2024
On 28/04/2024 10:17 PM, ryuukk_ wrote:
> |_| should be enough

``_`` is a valid identifier that may point to a length.

So that isn't usable.
April 30, 2024

On Sunday, 28 April 2024 at 16:08:11 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

On 28/04/2024 10:17 PM, ryuukk_ wrote:

>

|_| should be enough

_ is a valid identifier that may point to a length.

So that isn't usable.

I think it's better to represent the automatically calculated length using .. .

int[..] a = [1,2,3,4,5]
« First   ‹ Prev
1 2 3