digitalmars.D.learn - C-like static array size inference - how?
- arandomonlooker (20/20) Jun 06 2022 Hello.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (24/32) Jun 06 2022 As you already know, the correct syntax is 'int[]' :) but it won't work
- arandomonlooker (3/10) Jun 06 2022 Thank you. I was kind of distracted and i didn't type it
- Dennis (4/7) Jun 07 2022 Walter just improved it! https://github.com/dlang/dmd/pull/14181
- arandomonlooker (3/10) Jun 07 2022 There is any chance size inference is going to be implemented
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/8) Jun 07 2022 Do I remember correctly that there were syntax proposals that used $ or ...
- Mike Parker (5/10) Jun 07 2022 https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1039.md
- forkit (6/8) Jun 07 2022 That's a shame.
- Steven Schveighoffer (9/22) Jun 07 2022 I agree, it's a pain to dig out an import and the verbose name, vs. just...
- forkit (8/11) Jun 07 2022 I agree it's magic.
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): ```c int numbersINeed[] = {1, 2, 3, 4, 5}; ``` I usually transcribe them as follows, because the previous syntax causes a compiler error: ```d 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.
Jun 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
Jun 06 2022
On Tuesday, 7 June 2022 at 00:20:31 UTC, Ali Çehreli wrote:On 6/6/22 17:04, arandomonlooker wrote:Thank you. I was kind of distracted and i didn't type it correctly. Your solution works.[...]syntax causes[...]As you already know, the correct syntax is 'int[]' :) but it won't work because -betterC cannot support dynamic array. [...]
Jun 06 2022
On Tuesday, 7 June 2022 at 00:20:31 UTC, Ali Çehreli wrote:Walter just improved it! https://github.com/dlang/dmd/pull/14181 Perhaps try a [nightly build](https://github.com/dlang/dmd/releases/tag/nightly)it's complaining about TypeInfo being absent.What an unfortunate error message! Trying writeln() causes equally weird error messages.
Jun 07 2022
On Tuesday, 7 June 2022 at 00:20:31 UTC, Ali Çehreli wrote:On 6/6/22 17:04, arandomonlooker wrote:There is any chance size inference is going to be implemented into D as a feature?[...]syntax causes[...]As you already know, the correct syntax is 'int[]' :) but it won't work because -betterC cannot support dynamic array. [...]
Jun 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
Jun 07 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.
Jun 07 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];
Jun 07 2022
On 6/7/22 9:17 PM, forkit wrote:On Wednesday, 8 June 2022 at 01:11:45 UTC, Mike Parker wrote: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))`....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];I like `$`. It's got a well-defined meaning, and already is somewhat magic. -Steve
Jun 07 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. -SteveI 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).
Jun 07 2022