Thread overview
Alternatives to pointers?
Sep 29, 2017
Jerry
Sep 29, 2017
Jonathan M Davis
Sep 29, 2017
Elronnd
Sep 30, 2017
Jerry
Sep 29, 2017
Dukc
Sep 30, 2017
bitwise
Sep 30, 2017
Jerry
Sep 30, 2017
Timon Gehr
Sep 30, 2017
Jerry
September 29, 2017
I miss ref variables, for the simple fact that using the square brackets with a ref variable doesn't access the pointer. Don't know how many times I've accidentially used a pointer as an array. Not very easy to catch especially if the object has pointers to other objects of the same type. Possibly adding something like this, if ref variables are off the table?

@noarray int* value;
value[x] = 10; // compile error, pointer is not an array.
September 28, 2017
On Friday, September 29, 2017 01:51:36 Jerry via Digitalmars-d wrote:
> I miss ref variables, for the simple fact that using the square brackets with a ref variable doesn't access the pointer. Don't know how many times I've accidentially used a pointer as an array. Not very easy to catch especially if the object has pointers to other objects of the same type. Possibly adding something like this, if ref variables are off the table?
>
> @noarray int* value;
> value[x] = 10; // compile error, pointer is not an array.

You could always create a wrapper struct for the pointer and then not overload opIndex.

- Jonathan M Davis

September 29, 2017
On Friday, 29 September 2017 at 03:37:53 UTC, Jonathan M Davis wrote:
> You could always create a wrapper struct for the pointer and then not overload opIndex.

...alternatively, you could create a wrapper struct for the pointer, do 'alias this' on said pointer, and then overload opIndex to call static assert(0);.  Like this:


struct Ptr(T) {
    private T *_ptr;
    alias _ptr this;

    this(T *val) {
        _ptr = val;
    }

    void opIndex(size_t index)() {
        static assert(0);
    }
}
September 29, 2017
On Friday, 29 September 2017 at 01:51:36 UTC, Jerry wrote:
> Don't know how many times I've accidentially used a pointer as an array.

Using @safe let's the compiler to catch that. Well, in it you can't use pointer arithmetic even explicitly, but if you have many elements to point at you're usually better off using a real array anyway.
September 30, 2017
On Friday, 29 September 2017 at 03:37:53 UTC, Jonathan M Davis wrote:
> On Friday, September 29, 2017 01:51:36 Jerry via Digitalmars-d wrote:
>> I miss ref variables, for the simple fact that using the square brackets with a ref variable doesn't access the pointer. Don't know how many times I've accidentially used a pointer as an array. Not very easy to catch especially if the object has pointers to other objects of the same type. Possibly adding something like this, if ref variables are off the table?
>>
>> @noarray int* value;
>> value[x] = 10; // compile error, pointer is not an array.
>
> You could always create a wrapper struct for the pointer and then not overload opIndex.
>
> - Jonathan M Davis

Not the most ideal solution, doesn't work with several other features like auto.

September 30, 2017
On Friday, 29 September 2017 at 01:51:36 UTC, Jerry wrote:
> [...]

Maybe this?

ref auto at(T : U[], U)(T arr, size_t index) {
    return arr[index];
}

int main(string[] argv)
{
    int* a = new int(1);
    int[] b = [1];
    int[1] c = [1];
    a.at(0); // won't compile
    b.at(0);
    c.at(0);
    reutnr 0;
}
September 30, 2017
On 29.09.2017 03:51, Jerry wrote:
> I miss ref variables, for the simple fact that using the square brackets with a ref variable doesn't access the pointer. Don't know how many times I've accidentially used a pointer as an array. Not very easy to catch especially if the object has pointers to other objects of the same type. Possibly adding something like this, if ref variables are off the table?
> 
> @noarray int* value;
> value[x] = 10; // compile error, pointer is not an array.

void main()@safe{
    int x;
    int* value;
    value[x]=10;
}

Error: safe function 'D main' cannot index pointer 'value'
September 30, 2017
On Saturday, 30 September 2017 at 08:27:26 UTC, Timon Gehr wrote:
> On 29.09.2017 03:51, Jerry wrote:
>> I miss ref variables, for the simple fact that using the square brackets with a ref variable doesn't access the pointer. Don't know how many times I've accidentially used a pointer as an array. Not very easy to catch especially if the object has pointers to other objects of the same type. Possibly adding something like this, if ref variables are off the table?
>> 
>> @noarray int* value;
>> value[x] = 10; // compile error, pointer is not an array.
>
> void main()@safe{
>     int x;
>     int* value;
>     value[x]=10;
> }
>
> Error: safe function 'D main' cannot index pointer 'value'

Need it in unsafe/trusted code though.
September 30, 2017
On Saturday, 30 September 2017 at 04:15:52 UTC, bitwise wrote:
> On Friday, 29 September 2017 at 01:51:36 UTC, Jerry wrote:
>> [...]
>
> Maybe this?
>
> ref auto at(T : U[], U)(T arr, size_t index) {
>     return arr[index];
> }
>
> int main(string[] argv)
> {
>     int* a = new int(1);
>     int[] b = [1];
>     int[1] c = [1];
>     a.at(0); // won't compile
>     b.at(0);
>     c.at(0);
>     reutnr 0;
> }

That is interesting, it would mean not using "[]" or "[..]", which would kind of suck.