Out parameters and initialization

Ivan Senji ivan.senji_REMOVE_ at _THIS__gmail.com
Sun Feb 26 16:41:04 PST 2006


Unknown W. Brackets wrote:
> Given the following example:
> 
> class X
> {
>    int bar;
> }
> 
> int foo(out X var)
> {
>    var.bar = 5;
> 
>    assert(var.bar == 5);
> }
> 
> Look at that, an out parameter - being read?  Amazing, I've done the 
> unthinkable.

Don't see what the problem is, you as an author of foo know what you are 
doing so know if there should be only one dummy variable or more.

> 
> If a variable is a pointer, it should be a pointer.  

Agree.

> Classes, as 
> reference types, are clear because they are always pointers.  

Agree.

> However, 
> consider the following:
> 
> import std.stdio;
> 
> int main()
> {
>     int x;
>     test(x);
> 
>     return 0;
> }
> 
> void test(out int x)
> {
>     writefln(typeid(typeof(x)));
> }
> 
> This code clearly identifies x as an int.  If it is an int, it should be 
> an int, and it should always be an int.  I mean, that's just common 
> sense.  If an animal is a moose, it should not be a giraffe.  We learn 
> this in kindergarten.

Well in this case it is a moose&. D handles references a little strange 
meaning they aren't really real types, but they are still here. An out 
parameter means int is passed by reference.

You should think about it as: void test(int& x) in C++.

> 
> In every other way, out and inout parameters are treated as moose.  

Not really. Can the original moose changed when you pass it as in? No. 
Can it be changed when you pass it as out, or inout. Yes. So they aren't 
  really treated the same.

> I've really grown quite tired with your patronizing and condescending 
> tone, and you are reminding me exactly what I dislike about this 
> newsgroup and therefore D; the people who seem to surround it.  The 
> language seems okay but I honestly remember why I don't use it as often 
> every time I get in a conversation with one of you people.

I am sorry if anything I said seams patronizing or condescending. I can 
assure you I didn't mean it that way. I was just trying to explain my 
point of view. Maybe my imperfect English is a cause for occasional bad 
choise of words that can be misinterpreted.

Once again. I am sorry. Maybe it is best to stop the conversation.



More information about the Digitalmars-d-bugs mailing list