[Adding cross-post to C#. After all, there's no need to avoid
knowledgable people on this subject. As an introduction for C#
newsgroup people, Dale and I are discussing whether C# really has pass
by reference. The question boils down to whether the keyword "ref"
should be considered a part of the actual and formal parameters, or
simply part of the syntax.]
Dale King said:
The difference is which one is the one that could vary. Your way sounds like
you are saying it is PBR because the language sets the lvalue of the actual
parameter to be the same as the lvalue of the formal parameter.
I see. I certainly didn't intend for it to sound that way. As far as I
can tell, we are in complete agreement here except for my debatably
confusing use of the English language.
Dale King said:
Consider if you called a method using foo( ref i ); The question is what is
the actual parameter to the method. Is the parameter i or is it the
expression "ref i"? We could probably debate it for some time. If we go with
the notion that the actual parameter is ref i then that expression has no
lvalue (at least to my knowledge but I'm not a C# expert). In my way of
thinking the parameter is the expression ref i which produces an rvalue
which is the lvalue of the expression i.
Frankly, I think that's an incorrect way of looking at it. AFAIK (and
I'm no expert either, but...) there is no such thing in C# as an
expression "ref i". Although the Microsoft Press copy of the C# laguage
spec that I'm reading infuriatingly lacks an index (what were they
thinking?!?), it appears at first glance that the only uses of the ref
keyword are as a part of the grammars for the productions of "argument"
and "parameter-modifier" (which is used in "fixed-parameter"). The ref
keyword is not an operator that produces its own sort of expression.
So when you say:
I think before you talk about hidden and non-hidden pass by reference you
have to clarify your definitions of actual parameter. To call this pass by
reference to me is changing the established definition for what that means.
According to the book I use, "the expressions passed into those parameters
at the point of invocation are the actual parameters."
I think this is really just pushing the dispute onto a different word.
In C#, "ref i" isn't an expression, so the expression passed in at the
point of invocation must just be "i". I think there's definite good
reason for trusting the language spec's refusal to classify "ref i" as
an "expression" as well; namely, that it's illegal to use it in *any*
other context aside from a method invocation.
If there are other uses of 'ref i' which can obtain a pointer to some
variable (and there may be, as I'm definitely far from all-knowing about
C#), then I'd be wrong. But if, as I seem to be finding, "ref" is just
a required keyword that declares to the compiler that you know pass-by-
reference is happening, then I'd still call it pass-by-reference.
It's also interesting to read where section 1.3 discusses the subject
(and admittedly, it doesn't seem to be a normative piece of the
specification, nor is use of terms by the specification necessarily
consistent with their common usage, but nevertheless.) If you make some
simple replacements for clearer terminology:
"variable" -> "lvalue"
"parameter" -> "formal parameter"
"argument" -> "actual parameter"
Then this paragraph reads like the definition of pass by reference. As
far as I can tell, the only debatable replacement above is "variable" to
"lvalue"
Finally,
Consider also a similar thing in C. If I called foo( &i ) is the actual
parameter i or is it the expression &i? In this case &i has an lvalue but no
rvalue. I think you would be hard pressed to make a case for saying the
parameter is actually i. And I see little difference between the two.
Again, the difference here is that &i in C really is an expression.
That doesn't appear to be true for "ref i" in C#.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation