July 14, 2022

On Thursday, 14 July 2022 at 19:57:24 UTC, Basile B. wrote:

>

On Thursday, 14 July 2022 at 17:57:13 UTC, Ali Çehreli wrote:

>
  • int[$] // Not perfect because the array does not have a length

I wanted to implement that in my language and found a subtle problem that makes the implementation of the feature a not so nice looking special case : the semantics of int[$] require the initializer unlike auto, which can be directly replaced by the initializer type. That breaks the binary and (somewhat ideal) approach of either auto and replace or full type and implicit conv of the initializer.

dmd already comes very close, though:

unittest {
    int[2] arr = [1, 2, 3];
}

This fails to compile with "Error: mismatched array lengths, 2 and 3". So, treat $ in int[$] like it's a random number, which already works, but then when you get to checking the lengths, accept 3 as the length of the array.

July 14, 2022
On Thursday, 14 July 2022 at 19:39:17 UTC, H. S. Teoh wrote:
> On Thu, Jul 14, 2022 at 12:18:34PM -0700, Ali Çehreli via Digitalmars-d wrote:
>> On 7/14/22 11:55, H. S. Teoh wrote:
>> 
>> > Maybe int[static array] would make it much more obvious to newbies. ;-)
>> 
>> Looks good but 'array' is a legal identifier. :( Luckily nothing some creative acrostic can't fix:
>> 
>>   int[static alias,ref,return,assert,'y'] arr;
> [...]
>> Ok, too much silliness for one day. I stop here.
> [...]
>
> But but but... this is proposed syntax for automatically detecting the length of a static array.  At the very least, it should spell out this fact:
>
> 	int[auto static pragma(array, length)] arr;
>
> The pragma, of course, is to provide a context where legal identifiers have special meaning. :-D
>
> On a serious note, though, int[$] is ideal.
>
>
> T

auto[staticsupercalifragilisticexpialidocious:)] = [1, 2, 3];


July 15, 2022

On Thursday, 14 July 2022 at 17:46:18 UTC, Dave P. wrote:

>

Yes, I forgot to write the other half of my comment. I am also annoyed by this behavior, but I would be able to work around it if there was a convenient way to specify a static array of inferred length. Ansi C has this, D does not.

There was a DIP for it, but the author withdrew it. Anyone is free to pick up where he left off:

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

July 15, 2022

On Thursday, 14 July 2022 at 14:37:34 UTC, ryuukk_ wrote:

>

On Thursday, 14 July 2022 at 13:30:58 UTC, jfondren wrote:

>

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

>

It is misleading, nobody expect this to be GC allocated

People expect that who try to append to it later on in the function. The book "Learning D" really emphasizes stuff like this, useful but potentially surprising parts of the native types of the language.

Then the compiler should tell them that it is a static array and they should be explicit if they want to append

Corner cases from clueless people shouldn't make the default useless and misleading

>

All languages have stuff like this. D has a performance hazard in treating dynamic arrays like stacks; Go avoids that while having the fun design where appending to a slice might modify a different slice.

I disagree hard, not "all languages have stuff like this" D has stuff like this

Maybe Java/C# but they don't have the concept of static arrays, again, who's the target audience of D?

C# certainly has it and since 7.0 they can be called from safe contexts with Span<> types, your expectations can be fulfilled with

   Span<int> arr = stackalloc int[] {1, 2, 3};

Godbolt link, https://godbolt.org/z/6b6MMPe7v

While Java is getting them when Valhala finally gets integrated.

July 15, 2022

On Friday, 15 July 2022 at 02:14:53 UTC, Mike Parker wrote:

>

On Thursday, 14 July 2022 at 17:46:18 UTC, Dave P. wrote:

>

Yes, I forgot to write the other half of my comment. I am also annoyed by this behavior, but I would be able to work around it if there was a convenient way to specify a static array of inferred length. Ansi C has this, D does not.

There was a DIP for it, but the author withdrew it. Anyone is free to pick up where he left off:

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

I can hear Walter yelling for the DIPs 7 years ago: Again? No!

>

This change breaks existing code, requires an awkward workaround for existing uses, and has only a marginal benefit. At this point, I'm opposed to it.

It was written by Walter 10 years ago 😀

SDB@79

July 15, 2022

On Friday, 15 July 2022 at 05:50:33 UTC, Paulo Pinto wrote:

>
   Span<int> arr = stackalloc int[] {1, 2, 3};

It's a very long C# code, but they're right on this rationale for D:

>

The programmer must remember to update the integer literal in the static array declaration from 2 to 3, else face a compiler error.

SDB@79

July 15, 2022

On Friday, 15 July 2022 at 06:17:26 UTC, Salih Dincer wrote:

>

On Friday, 15 July 2022 at 02:14:53 UTC, Mike Parker wrote:

>

On Thursday, 14 July 2022 at 17:46:18 UTC, Dave P. wrote:

>

Yes, I forgot to write the other half of my comment. I am also annoyed by this behavior, but I would be able to work around it if there was a convenient way to specify a static array of inferred length. Ansi C has this, D does not.

There was a DIP for it, but the author withdrew it. Anyone is free to pick up where he left off:

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

I can hear Walter yelling for the DIPs 7 years ago: Again? No!

But now you can just point out that C++ does it...

July 16, 2022

On Thursday, 14 July 2022 at 13:30:58 UTC, jfondren wrote:

>

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

>

It is misleading, nobody expect this to be GC allocated

People expect that who try to append to it later on in the function.

And this also should be killed with fire. We need a clean distinction between static arrays, GC-controlled dynamic arrays, and slices.

July 16, 2022

On Saturday, 16 July 2022 at 15:12:19 UTC, Ogi wrote:

>

On Thursday, 14 July 2022 at 13:30:58 UTC, jfondren wrote:

>

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

>

It is misleading, nobody expect this to be GC allocated

People expect that who try to append to it later on in the function.

And this also should be killed with fire. We need a clean distinction between static arrays, GC-controlled dynamic arrays, and slices.

Correct but that's just one out of many examples where C++ is simply better designed. C++'s references work better than D's inout, C++'s const system works better as it allows for reference counted containers ("logical constness"), D's postblits had to be replaced with copy constructors (like C++ does it), shared doesn't mean anything (C++ lacks it), D's class construct introduces a value vs pointer semantics schism that C++ consciously avoided, D's class construct comes with a monitor (C++ does not have this overhead), C++ does not misuse the enum keyword, C++ lacks an effect system like @nogc @safe @etc that simply gets in the way, C++ doesn't need "interface", it allows for multiple inheritance instead so that's an entire entity that it lacks as it doesn't need it.

Now you can disagree with this list all you want and point out details which I got wrong but the point here is: Don't be surprised that D never really replaced C++, it's not the lack of marketing and it's not the lack of manpower either, the language is not designed all that well and it didn't learn much from C++.

July 16, 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]

TDPL book deals with this question. Andrei said that "experience with pascal" shows it's better to be dynamic by default.

>

Also why it is GC allocated without requiring new?

Otherwise it would have to be either immutable or impure. Consider:

@safe pure int[] fun()
{ auto arr = [1, 2, 3];
  return arr;
}

@safe void main()
{ import std.stdio;
  auto arr1 = fun();
  foreach(ref el; arr1) el += 3;
  auto arr2 = fun();
  foreach(ref el; arr2) el += 10;
  arr1.writeln;
  arr2.writeln;
}

GC allocation is the only way this can work. If arr was allocated from static data, the two calls to fun would refer to the same array, meaning the output would be

[11, 12, 13]
[11, 12, 13]

. fun would not be pure at all!

arr in fun also can not be stack allocated, because stack allocations can last only until the function that allocated returns. arr is returned, meaning it needs to continue existing beyond returning from fun.