July 16, 2022

On Thursday, 14 July 2022 at 15:26:44 UTC, Dave P. wrote:

>

D has no way to express a similar concept without being horribly verbose with .staticArray.

import std : sa = staticArray;
//or if you don't want to rename in the import:
alias sa = std.staticArray;

I admit it's annoying to do because it's so non-standard to rename stuff like this. I'd do this if I was using staticArray a lot, but not in the general case.

July 16, 2022

On Saturday, 16 July 2022 at 16:27:06 UTC, Dukc wrote:

>

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?

Matter turns elsewhere... Are GC-controlled D arrays better, or static arrays recommended by traditional programmers?

On the other hand, the last example also works with static array because it is being copied:

void main()
{
  //...

  auto foo = () => cast(int[3])[1, 2, 3];

  arr1 = foo();
  foreach(ref el; arr1) el *= 2;
  assert(arr1 == [2, 4, 6]);

  arr2 = foo();
  foreach(ref el; arr2) el *= 5;
  assert(arr2 == [5, 10, 15]);
}

SDB@79

July 16, 2022

On Saturday, 16 July 2022 at 16:44:16 UTC, Dukc wrote:

>

On Thursday, 14 July 2022 at 15:26:44 UTC, Dave P. wrote:

>

D has no way to express a similar concept without being horribly verbose with .staticArray.

import std : sa = staticArray;
//or if you don't want to rename in the import:
alias sa = std.staticArray;
  import std : sa = staticArray;
  auto sar = [1, 2, 3].sa; // really, looks short&easy

Why can't we use this with the byte type?

  auto arr = sa!(long, [1, 2, 3]);
  assert(is(typeof(arr) : long[3])); // ok

  //auto noCompile = sa!(byte, [1, 2, 3]);

SDB@79

July 16, 2022

On Saturday, 16 July 2022 at 18:00:40 UTC, Salih Dincer wrote:

>

Why can't we use this with the byte type?

  auto arr = sa!(long, [1, 2, 3]);
  assert(is(typeof(arr) : long[3])); // ok

  //auto noCompile = sa!(byte, [1, 2, 3]);

Because the literal [1, 2, 3] is considered an int array literal, not a byte array literal.

It works if you use an explicit cast:

auto arr = staticArray(cast(byte[]) [1, 2, 3]);
static assert(is(typeof(arr) == byte[3])); // ok
July 16, 2022
On Thursday, 14 July 2022 at 18:29:43 UTC, H. S. Teoh wrote:
> On Thu, Jul 14, 2022 at 10:57:13AM -0700, Ali Çehreli via Digitalmars-d wrote:
>> [1] Other than bike-shedding:
>> 
>> - int[$]      // Not perfect because the array does not have a length
>>               // (yet) but $ means exactly that in other contexts.
>
> IMO, this is the best bikeshed color. :-P

Agreed.

> But anyway, this is by far not the first time this issue has been brought up, and it's high time we made some progress on it.  Where's the DIP when you need it?  Actually, I'm almost certain somebody already wrote the DIP, and IIRC even implemented it in a dmd branch.  We should resurrect the DIP/implementation and push it through.  It has been too many years.

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

-Steve
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]

Also why it is GC allocated without requiring new?

That would be a little confusing. If i were writing a language or compiler; if it was a slice/array without a known length it would allocate, if it is a known length or on the stack it would copy to the stack from a fixed immutable array in ROM, and in the case it's immutable it would just point to the array it would have copied from.

i know enum array values would likely always be forcibly allocated either way.

And static/module immutable items could point to the original ROM data as it would be allocated during compile-time.

July 16, 2022

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

>

Also why it is GC allocated without requiring new?

Its easier to write average code without always thinking about allocations. Its optimized for average coder.

July 17, 2022

On Saturday, 16 July 2022 at 22:44:07 UTC, welkam wrote:

>

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

>

Also why it is GC allocated without requiring new?

Its easier to write average code without always thinking about allocations. Its optimized for average coder.

Yes, this could be done as an optimization, if people want to enforce it then just let them add @nogc.

July 17, 2022

On Saturday, 16 July 2022 at 18:59:52 UTC, Paul Backus wrote:

>

Because the literal [1, 2, 3] is considered an int array literal, not a byte array literal.

It works if you use an explicit cast:

auto arr = staticArray(cast(byte[]) [1, 2, 3]);
static assert(is(typeof(arr) == byte[3])); // ok

Thank you, but this complicates things! Because it makes using staticArray unnecessary:

  auto def = sa(cast(char[3]) [100, 101, 102]);
  assert(is(typeof(def) : char[3]));

  import std.stdio;

  auto cdef = cast(char[4]) [99, 100, 101, 102];
  cdef.writeln; // "cdef"

I think DIP1039 should be opened for review in the next version urgently.

SDB@79

July 17, 2022

On Sunday, 17 July 2022 at 07:04:01 UTC, Salih Dincer wrote:

>

I think DIP1039 should be opened for review in the next version urgently.

Why urgent? Nothing prevents you from creating your own ‘mkarray!char(1,2,3,4)’?

The focus should be on things that matters, more special syntax is not a net positive.

New features ought to be more generic, like deduction guides in C++.