November 30, 2022

On Tuesday, 29 November 2022 at 18:59:46 UTC, DLearner wrote:

>

To me, it appears that there are really two (entirely separate) concepts:

A. Supporting the useful concept of variable length (but otherwise entirely conventional) arrays;
B. Supporting a language feature that acts as a window to an array, through which that array can be manipulated.

And currently these two concepts are combined.

Suggestion: it would be clearer if the two concepts were separated:

  1. Convert 'int[] VarArr;' so it produces a straightforward value-type variable array, called 'VarArr';
  2. Implement a new concept 'int slice Window;' to produce an object of type 'int slice', called 'Window'.
    'Window' is a 'slice' into an int array, not an array itself or even a variable.

Opinions?

I have implemented that in styx.

  1. You have the type for dynamic arrays, called TypeRcArray, syntax is Type[+]
  2. You have the type for slices (what you describe as a window), syntax is Type[]
    but it is mostly obtained using expressions, e.g mySlice = myRcArray[lo .. hi] or
    myStaticArray[lo .. hi] or myPointer[lo .. hi].

This sounded like a good idea but it has appeared very quickly that slices are not so useful, especially when management is based on reference counting because then slices requires a form of management too. Here is why:

Main caracteristics of a slice are

  • they cannot modify the identity of their sources. The identity of the source is what makes the integrity of a dynamic array, what makes their references countable. So it is the content pointer and the length. In consequence you cannot change the length of the source, you can only reduce the view. You can change the elements in the view.
  • the length and the pointer are cached as a value on the stack while for a dynamic array this is stored before that data, on the heap.

Problems start happening when you escape a slice

struct S
{
    var s32[] memberSlice;
}

function f(var S s): auto
{
    var s32[+] a = (new s32[+])(2);

    // problem 1 : `s` lifetime > `a` lifetime
    s = (a[]).tupleof;  // note: tuples are used in pace of struct literals

    // problem 2
    return a[1 .. $]; // a is automatically decref'd on return
                      // so the caller pulls a dead heap block.
}

Essentially slices are only useful to be consumed locally, typically

while mySlice.length do
{
   slice = slice[1..$];
}

And that's it. So at first glance slices are some cool, simplified, functionally stripped down arrays but they introduce new problems, at least when management of dynamic arrays is based on reference counting. Those new problems can only be solved using lifetime analysis (so a compile-time check... still better than runtime ref counting however).

November 30, 2022

On Wednesday, 30 November 2022 at 03:04:47 UTC, Basile B. wrote:

>

Essentially slices are only useful to be consumed locally, typically

while mySlice.length do
{
   slice = slice[1..$];
}

sorry I cant force push, it was obviously meant to be written as

while mySlice.length do
{
   mySlice = mySlice[1..$];
}
November 30, 2022
On 11/29/22 15:25, DLearner wrote:

> 'dynamic array' is
> not a reasonable description for a construct that behaves like
> VarArr2[3] becoming 40.

I agree with you: It has always bothered me to call the following a dynamic array:

  int[] arr;

'arr' is not a dynamic array but the slice interface. Dynamic arrays are owned by the D runtime and are always nameless.

Ali

December 03, 2022

On Wednesday, 30 November 2022 at 02:29:03 UTC, Paul Backus wrote:

[...]

>

If you want a dynamic array with value semantics, you should use a library-defined container type (e.g., struct DynamicArray).

I agree should not change existing meaning of

int[] A;

But why not allow a construct for value-type variable arrays like:

int[*] B;

Best regards

December 04, 2022

On Saturday, 3 December 2022 at 22:46:31 UTC, DLearner wrote:

>

I agree should not change existing meaning of

int[] A;

But why not allow a construct for value-type variable arrays like:

int[*] B;

There's no reason to add more complexity to the language for this when the same result can already be achieved using existing language features.

December 04, 2022

On Wednesday, 30 November 2022 at 03:04:47 UTC, Basile B. wrote:

>

I have implemented that in styx.

  1. You have the type for dynamic arrays, called TypeRcArray, syntax is Type[+]
  2. You have the type for slices (what you describe as a window), syntax is Type[]
    but it is mostly obtained using expressions, e.g mySlice = myRcArray[lo .. hi] or
    myStaticArray[lo .. hi] or myPointer[lo .. hi].

This sounded like a good idea but it has appeared very quickly that slices are not so useful...

Recently DIP1044 was published about enum and although we can use with() instead we waste time unnecessarily...

I've never tried it, but the feature shown below is not available in D! Why not such good things?

enum Foo
{
     a0 = 1,
     a1
}

enum Bar: Foo
{
    a2 // 3
}

Thanks...

SDB@79

December 05, 2022

On Sunday, 4 December 2022 at 09:54:53 UTC, Salih Dincer wrote:

>

Recently DIP1044 was published about enum and although we can use with() instead we waste time unnecessarily...

With cannot be used in calls.
Hence when calling a function you need to either spell out the enum type or wrap the call in a with or multiple withs.

>

I've never tried it, but the feature shown below is not available in D! Why not such good things?

enum Foo
{
     a0 = 1,
     a1
}

enum Bar: Foo
{
    a2 // 3
}

Thanks...

SDB@79

This is easy to add and costs almost no complexity

1 2
Next ›   Last »