Thread overview
C-like static array size inference - how?
Jun 07, 2022
arandomonlooker
Jun 07, 2022
Ali Çehreli
Jun 07, 2022
arandomonlooker
Jun 07, 2022
Dennis
Jun 07, 2022
arandomonlooker
Jun 08, 2022
Ali Çehreli
Jun 08, 2022
Mike Parker
Jun 08, 2022
forkit
Jun 08, 2022
forkit
June 07, 2022

Hello.
I am working on a project related to low-level development as a beginner, and i decided to pick D as the most optimal programming language for that, in large part because of it's strong integration with C and C++.
I happen to have a lot of arrays that i want to translate to D, formulated as follows (it's a unrelated example):

int numbersINeed[] = {1, 2, 3, 4, 5};

I usually transcribe them as follows, because the previous syntax causes a compiler error:

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

As i'm using the betterC mode, it's complaining about TypeInfo being absent. Can't i use some feature to imply that D must deduct the size from the array i am assigning, like an underscore? Can't D do that?
Thanks in advance to you all.

June 06, 2022
On 6/6/22 17:04, arandomonlooker wrote:

> I usually transcribe them as follows, because the previous syntax causes
> a compiler error:
>
> ```d
> int numbersINeed[] = [1, 2, 3, 4, 5];
> ```

As you already know, the correct syntax is 'int[]' :) but it won't work because -betterC cannot support dynamic array.

> it's complaining about TypeInfo being absent.

What an unfortunate error message! Trying writeln() causes equally weird error messages.

> Can't i use some feature to imply that D must deduct the size
> from the array i am assigning, like an underscore? Can't D do that?

That request comes up relatively frequently. Currently, the only way I know of is to use std.array.staticArray:

import std.array;

extern (C)
void main() {
  auto numbersINeed = staticArray([1, 2, 3, 4, 5]);

  import std.range;
  import std.algorithm;
  import core.stdc.stdio;

  //          |
  //          V
  numbersINeed[].each!(n => printf("%d ", n));
}

Aside: I printed the elements with a range algorithm, which is not necessary at all. However, the reason I had to use it is because static arrays are not ranges because their lengths cannot change. [] takes a slice to all elements, which can reduce its length, and accordingly is a range.

Ali

June 07, 2022
On Tuesday, 7 June 2022 at 00:20:31 UTC, Ali Çehreli wrote:
> On 6/6/22 17:04, arandomonlooker wrote:
>
> > [...]
> syntax causes
> > [...]
>
> As you already know, the correct syntax is 'int[]' :) but it won't work because -betterC cannot support dynamic array.
>
> [...]

Thank you. I was kind of distracted and i didn't type it correctly. Your solution works.
June 07, 2022

On Tuesday, 7 June 2022 at 00:20:31 UTC, Ali Çehreli wrote:

> >

it's complaining about TypeInfo being absent.

What an unfortunate error message! Trying writeln() causes equally weird error messages.

Walter just improved it! https://github.com/dlang/dmd/pull/14181

Perhaps try a nightly build

June 07, 2022
On Tuesday, 7 June 2022 at 00:20:31 UTC, Ali Çehreli wrote:
> On 6/6/22 17:04, arandomonlooker wrote:
>
> > [...]
> syntax causes
> > [...]
>
> As you already know, the correct syntax is 'int[]' :) but it won't work because -betterC cannot support dynamic array.
>
> [...]

There is any chance size inference is going to be implemented into D as a feature?
June 07, 2022
On 6/7/22 16:38, arandomonlooker wrote:

> There is any chance size inference is going to be implemented into D as a feature?

Do I remember correctly that there were syntax proposals that used $ or _?

  int[$] arr = [ 1, 2 ];
  int[_] arr = [ 1, 2 ];

But I can't find past discussions about that.

Ali
June 08, 2022
On Wednesday, 8 June 2022 at 00:43:24 UTC, Ali Çehreli wrote:

>
> Do I remember correctly that there were syntax proposals that used $ or _?
>
>   int[$] arr = [ 1, 2 ];
>   int[_] arr = [ 1, 2 ];
>
> But I can't find past discussions about that.

https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1039.md

Links to the discussion and feedback threads are in the review summary. The author withdrew the DIP, so anyone who would like to pick it up again is free to do so.
June 08, 2022
On Wednesday, 8 June 2022 at 01:11:45 UTC, Mike Parker wrote:
>
> ...The author withdrew the DIP ....
> ..

That's a shame.

Seems like a useful language feature. I'd be using it already if it existed.

I'd have gone for:

int[..] arr = [1,2,3];
June 07, 2022

On 6/7/22 9:17 PM, forkit wrote:

>

On Wednesday, 8 June 2022 at 01:11:45 UTC, Mike Parker wrote:

>

...The author withdrew the DIP ....
..

That's a shame.

Seems like a useful language feature. I'd be using it already if it existed.

I agree, it's a pain to dig out an import and the verbose name, vs. just putting in the $. Not to mention, I don't know if 2-dimensional or n-dimensional static arrays are easy to capture with a library call + literal.

These are the kinds of things that make D pleasant. Like V[K2][K1] being more intuitive than HashMap!(K1, HashMap!(K2, V)).

>

I'd have gone for:

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

I like $. It's got a well-defined meaning, and already is somewhat magic.

-Steve

June 08, 2022
On Wednesday, 8 June 2022 at 01:32:42 UTC, Steven Schveighoffer wrote:
>
> I like `$`. It's got a well-defined meaning, and already is somewhat magic.
>
> -Steve

I agree it's magic.

warray[5..$]  - this is one of the best uses of syntax magic in D!

I think it's what first attracted me to the language actually.

A little more 'magic', and this could be possible:

int[n] myArray = [1,2,3];

( I think I like the use of 'n' more than '..' or '$' actually).