In, inout, out, and damn lies....

S. Chancellor dnewsgr at mephit.kicks-ass.org
Wed Feb 22 18:08:39 PST 2006


On 2006-02-18 02:34:24 -0800, "Regan Heath" <regan at netwin.co.nz> said:

> Please elaborate. I don't see how this:
> 
> struct A {}
> void foo(A* p) {}
> 
> is any more or less vague than this:
> 
> struct A {}
> void foo(byref A p) {}

That's because that's not what I asked for.  I asked for something that 
was InByRef or whatever is agreed upon.    Just ByRef Is just as vague.

> 
>> Not only that, D set the precedent for new parameter passing syntax 
>> with  in/inout/out and you need to break from that to use pointers.
> 
> 'out' and 'inout' do replace the use of pointers (or passing by 
> reference)  for 2 cases of parameter, that is true. By I do not think 
> that constitutes  a precedent, it's simply a better way to handle each 
> of those cases. The  same cannot be said for "byref", it's not any 
> _better_ than a pointer.
> 
>> Thirdly D also set a precedent by treating pointers as a new type (D  
>> changed declarations so that pointers are with the type rather than the 
>>  instance now.)
> 
> Pointers have always been a distinct type. The change D made was in how 
>  the declaration of types is parsed, nothing more.
> 
>> Presuming that a int* is a different type
> 
> It is.
> 
>> , then what you are saying is that you want something other than an int 
>>  to manipulate. That's untrue though.
> 
> I'm not sure what you meant by this statement. Can you re-phrase please.

My argument is that asking for an Type* means you want to work on an 
Type*. Usually you just want to work on the Type, not actually it's 
location.

> 
>> So, when using a pointer in D you are doing a few things.  You are 
>> being  vague about what you do to the original value.
> 
> Not any more or less vague than a class reference. Both a pointer and a 
>  class reference are passed 'in' and you cannot effect their value 
> outside  the function call. Therefore the original reference/pointer is 
> safe from  manipulation. In neither case is the content of the 
> class/struct safe from  manipulation. eg.
> 
> struct A { int a; }
> class B  { int b; }
> 
> void foo(A* a) { a.a = 1; }
> void foo(B b)  { b.b = 1; }
> 
> void main()
> {
>    A a;
>    B b = new B();
>    foo(&a);
>    foo(b);
> }
> 
> the two cases above are essentially identical, both functions accept a  
> reference to the data type they wish to access, both references are 
> passed  'in'. The only difference is that A is a value type and to get 
> a reference  you must use a pointer.

Of course, I'm not arguing about that.  I want something that SAYS the 
structure is still being unmanipulated.  Whether walter could implement 
something like that or not is irrelevant to me.

> 
>> You also want to deal with a pointer to an int, and not actually an 
>> int.  While in some cases this is the case, most of the time all you 
>> wanted  was the value.
> 
> You have no need of pointers when passing an int:
>    - if you want to read it, you use 'in'.
>    - if you want to write to it, you use 'out'.
>    - if you want to read and write it, you use 'inout'.
> 
> In what case would you use "byref" with an 'int'? If you want to read,  
> write, or read+write it you'd use one of the existing modes 
> in/out/inout,  right?

Of course, which is why my original message used a structure as an 
example.  You are indeed correct that it's useless for an int, since 
the storage required is the same for a pointer.

> 
>> Note that you can have parameters of type in that are pointers,  since  
>> pointers are associated with variable types, and not passing method.
> 
> A pointer _is_ a variable type.

Indeed, which is why i want to define a passing method: "InRef"

-S.




More information about the Digitalmars-d mailing list