3 ms·
This makes sense, but then why the use of `=` for function arguments?
by _Wintermute 3y ago
This makes sense, but then why the use of `=` for function arguments?
- rcthompson 3y agoBecause in that context the equals sign makes sense, e.g. "Call function F with X equal to 5."
- _Wintermute 3y agoI don't see how that logic differs from using `=` as an assignment operator. "Assign X with value equal to 5"
- civilized 3y agoI think the argument is that, to the uninitiated, x = x + 2 looks more like an invalid equality than "replace the value stored in x with its current value plus 2". Personally, it seems to me that = for assignment is so ubiquitous that this argument has become stale. I still use the arrow because it's the prevailing recommendation in R style guides.
- rcthompson 3y agoWhen you do `f(x=5)`, x doesn't get set to 5 anywhere you can see (it happens inside the body of f, which you are treating as a black box). So logically, in the context of the code that's calling f, it's not an assignment operation. Admittedly this kind of goes out the window with a lot of tidyverse non-standard eval stuff. E.g. if you do `df %>% mutate(x = 5)`, then x does get set to 5 in the data frame, so that is logically an assignment of x in a visible scope using '='. Going by my logic, functions like mutate should ideally use a different symbol for operations that alter the input like this, but we run up against syntactic limitations of the language.