digitalmars.D - Alternatives to pointers?
- Jerry (8/8) Sep 28 2017 I miss ref variables, for the simple fact that using the square
- Jonathan M Davis (4/12) Sep 28 2017 You could always create a wrapper struct for the pointer and then not
- Elronnd (15/17) Sep 28 2017 ...alternatively, you could create a wrapper struct for the
- Jerry (4/19) Sep 29 2017 Not the most ideal solution, doesn't work with several other
- Dukc (5/7) Sep 29 2017 Using @safe let's the compiler to catch that. Well, in it you
- bitwise (15/16) Sep 29 2017 Maybe this?
- Jerry (3/19) Sep 30 2017 That is interesting, it would mean not using "[]" or "[..]",
- Timon Gehr (7/16) Sep 30 2017 void main()@safe{
- Jerry (2/19) Sep 30 2017 Need it in unsafe/trusted code though.
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
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
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
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:Not the most ideal solution, doesn't work with several other features like auto.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 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.
Sep 29 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; }
Sep 29 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:That is interesting, it would mean not using "[]" or "[..]", which would kind of suck.[...]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 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'
Sep 30 2017
On Saturday, 30 September 2017 at 08:27:26 UTC, Timon Gehr wrote:On 29.09.2017 03:51, Jerry wrote:Need it in unsafe/trusted code though.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