www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dereferencing void pointers: why not?

reply "Vladimir Panteleev" <thecybershadow gmail.com> writes:
The Pascal/Delphi language has a nice feature - it can operate (within l=
imits) on typeless data. This is similar to void pointers, but without t=
he pointer clutter (so is in effect "void references"). Some examples:

1) Using a void pointer reference

Unlike in C/C++/D, you can dereference a void pointer (which in Pascal i=
s represented by the "Pointer" type).
This allows me to write instead of:
  PInteger(P)^ :=3D 5;
this:
  Integer(P^) :=3D 5;

The difference in Pascal is more significant since you must define an in=
teger pointer type :)
In a C-like language, it would be from:
  *cast(int*)p =3D 5;
to
  cast(int)*p =3D 5;
Less asterisks, less strain to the eye :) This may become worth it when =
the cast expressions grow long and nested.

2) Typeless function parameters

You can write something like this:
  procedure Test(var Data);

Any mutable (non-const) value converts implicitely to that "void" type.
You can also use "const" instead of "var".

I guess in D it would be declaring such a function argument using "ref v=
oid Data", OSLT.

What can you do with such a reference? Well,
 - you can cast it to something immediately useable
 - you can get the address of it (void*)
 - you can pass it on to other functions

Thus, it's just a little bit of syntax sugar and flexibility that fits i=
n nicely in the type system. =


-- =

Best regards,
 Vladimir                          mailto:thecybershadow gmail.com
Sep 18 2007
parent Robert Fraser <fraserofthenight gmail.com> writes:
Almost like dynamic typing... Except instead of being friendly and smiling like
a duck it's scary and dangerous like an anteater.

Vladimir Panteleev Wrote:

 The Pascal/Delphi language has a nice feature - it can operate (within limits)
on typeless data. This is similar to void pointers, but without the pointer
clutter (so is in effect "void references"). Some examples:
 
 1) Using a void pointer reference
 
 Unlike in C/C++/D, you can dereference a void pointer (which in Pascal is
represented by the "Pointer" type).
 This allows me to write instead of:
   PInteger(P)^ := 5;
 this:
   Integer(P^) := 5;
 
 The difference in Pascal is more significant since you must define an integer
pointer type :)
 In a C-like language, it would be from:
   *cast(int*)p = 5;
 to
   cast(int)*p = 5;
 Less asterisks, less strain to the eye :) This may become worth it when the
cast expressions grow long and nested.
 
 2) Typeless function parameters
 
 You can write something like this:
   procedure Test(var Data);
 
 Any mutable (non-const) value converts implicitely to that "void" type.
 You can also use "const" instead of "var".
 
 I guess in D it would be declaring such a function argument using "ref void
Data", OSLT.
 
 What can you do with such a reference? Well,
  - you can cast it to something immediately useable
  - you can get the address of it (void*)
  - you can pass it on to other functions
 
 Thus, it's just a little bit of syntax sugar and flexibility that fits in
nicely in the type system. 
 
 -- 
 Best regards,
  Vladimir                          mailto:thecybershadow gmail.com

Sep 18 2007