[nem-en] out / ref

Igor Tkachev it at rsdn.ru
Wed Sep 27 07:06:32 CEST 2006


Hello Ivan,

>>> I don't like Foo(out _) and Foo(ref _), that will confuse developers absolutely.
>>I don't think so. Foo(out _)/Foo(ref _) with explicit out/ref is more than clear.
>  
> Yes, this will make using of Foo(out ...)/Foo(ref ...) more
> clear, but in this way Foo(_) should mean something the same. But in
> fact Foo(_) is absolutely different thing. I would
> prefer that Foo(out _)/Foo(ref _) will have the same behaviour, i.e.
> (x => Foo(out x))/(x => Foo(ref x)).

Good point. Fortunately, compiler does not support ref/out partial
substitution. But finally, by this example, you made me 100% sure that
the explicit ref/out is the right way. Thank you.

>>> Also how Foo(ref _) should works in such code:
>>> 
>>> Foo(ref t : A)
>>> {
>>> t.Method();
>>> }
>>
>>How it should work in such case?
>>
>>def a : A = null;
>>Foo(ref a);
>>?
>  
> In the most of situations I use 'ref' when I expect in 'Foo'
> more rational value then a simple 'null'.

It depends. For instance, I can expect that Foo either initializes my
variable or use the current value. So, your example should check it
first to be correct.

Anyway, we are discussing just preferences, things that we like or
don't.

> Yes your code is mistaken, but this is predictable error. When
> you will use Foo(ref _) you will make a potential error.

I don't see any difference.

Actually, potential error is the design which uses ref/out to return
more than one value. Thanks N, now we can use tuples instead. However
we still have to deal with some third party stuff like the following:

uint           itemID;
IVsUIHierarchy hierarchy;
IVsWindowFrame docFrame;
IVsTextView    textView;

VsShell.OpenDocument(Site, loc.File,
    NativeMethods.LOGVIEWID_Code, out hierarchy, out itemID, out docFrame, out textView);


Now compare with this:
    

IVsTextView textView;

VsShell.OpenDocument(Site, loc.File, NativeMethods.LOGVIEWID_Code, out _, out _, out _, out textView);
    

A little bit shorter and more readable, isn't it? And what is even
more important, it does not have any garbage local variables. So I do
not have to waste my time to check if they are used somewhere or not.
    
-- 
Best regards,
 Igor                            mailto:it at rsdn.ru






More information about the devel-en mailing list