www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alternatives to pointers?

reply Jerry <hurricane hereiam.com> writes:
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.
Sep 28 2017
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Sep 28 2017
next sibling parent Elronnd <elronnd em.slashem.me> writes:
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); } }
Sep 28 2017
prev sibling parent Jerry <hurricane hereiam.com> writes:
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.
Sep 29 2017
prev sibling next sibling parent Dukc <ajieskola gmail.com> writes:
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.
Sep 29 2017
prev sibling next sibling parent reply bitwise <bitwise.pvt gmail.com> writes:
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; }
Sep 29 2017
parent Jerry <hurricane hereiam.com> writes:
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.
Sep 30 2017
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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'
Sep 30 2017
parent Jerry <hurricane hereiam.com> writes:
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.
Sep 30 2017