Re: Alternatives to pointers?

2017-09-30 Thread Jerry via Digitalmars-d

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.


Re: Alternatives to pointers?

2017-09-30 Thread Jerry via Digitalmars-d

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.


Re: Alternatives to pointers?

2017-09-30 Thread Timon Gehr via Digitalmars-d

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'


Re: Alternatives to pointers?

2017-09-29 Thread bitwise via Digitalmars-d

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;
}


Re: Alternatives to pointers?

2017-09-29 Thread Jerry via Digitalmars-d
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.




Re: Alternatives to pointers?

2017-09-29 Thread Dukc via Digitalmars-d

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.


Re: Alternatives to pointers?

2017-09-28 Thread Elronnd via Digitalmars-d
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);
}
}


Re: Alternatives to pointers?

2017-09-28 Thread Jonathan M Davis via Digitalmars-d
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