Reason to must put 'ref' in both param and arg | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Reason to must put 'ref' in both param and arg

It is like a two-factor measure to ensure that it was intended to be ref, to avoid modify data that shouldn't, right? (in a sense of clean code and readability) Feel free to compliment with more info if so.

10th Dec 2021, 10:20 PM
Kiwwi#
Kiwwi# - avatar
2 Answers
+ 2
I can imagine that there is a deeper meaning to it. I think of it from two different perspectives: on the one hand there is the access from within the method. Irrespective of how the argument was passed or what argument was passed, the method knows the parameter is a reference, and so the value inside the method must be dereferenced first. From outside the method, however, when an argument is passed, it must be marked that a reference to the value is to be taken. From this perspective, there is no knowledge of what the method actually does. One might say there is an outside view and an inside view. Both have different reasons why a parameter is marked ref, or the arguemnt is passed as ref. Hence, both are necessary. I can imagine that it is possible to have the compiler figure this out and pass as ref if the parameter is marked as such. But there may be a choice NOT to do so. Perhaps readability and the reasons you gave might come into play there. Well, this is just my thought on it :)
12th Dec 2021, 10:04 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
+ 2
Agnes Ahlberg good point Conclusion: It lets the compiler know whats going on, and for the programmer on semanthic readability.
12th Dec 2021, 1:31 PM
Kiwwi#
Kiwwi# - avatar