@g-grothendieck's answer to this question inspired me to play with some assignment functions such as ==<-
or ><-
.
See the following :
`><-` <- function(e1,e2,value) replace(e1, e1 > e2, value)
x <- c(5,15)
x > 10 <- 42
x
# [1] 5 42
I can also define it for <
:
`<<-` <- function(e1, e2, value) replace(e1, e1 < e2, value)
x <- c(5,15)
x < 10 <- 42
x
# [1] 42 15
But the problem is that now the <<-
operator is redefined and this doesn't work anymore :
x <<- "hello"
Error in replace(e1, which(e1 < e2), value) : argument "value" is missing, with no default
Interestingly x < y <- z
calls <<-
even when it's not been overwritten.
rm(`<<-`)
x < 10 <- 42
Error in x < 10 <- 42 : incorrect number of arguments to "<<-"
Would there be a way to keep the original behavior of <<-
while still defining this custom behavior ?
An expression is said to be ambiguous (or poorly defined) if its definition does not assign it a unique interpretation or value. An expression which is not ambiguous is said to be well-defined.
An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class. This can be solved by using a virtual base class.
Ambiguity in inheritance can be defined as when one class is derived for two or more base classes then there are chances that the base classes have functions with the same name. So it will confuse derived class to choose from similar name functions. To solve this ambiguity scope resolution operator is used “::”.
This seems to work :
`<<-` <- function(e1, e2, value){
if(missing(value))
eval.parent(substitute(.Primitive("<<-")(e1, e2)))
else
replace(e1, e1 < e2,value)
}
x <- c(5,15)
x < 10 <- 42
x
# [1] 42 15
x <<- "hello"
x
# [1] "hello"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With