Search code examples
roperatorsassignment-operator

Forward arrow operator not found


I'm confused with the nature of ->, I cannot get its definition like I can with other operators and it doesn't behave like <-. See below:

print(`<-`) # .Primitive("<-")
print(`->`) # Error in print(`->`) : object '->' not found

Also, I can't hijack it, though R won't fire any error if I try to :

`->` = `+`  # attempting to hijack `->`, no error
print(`->`) # function (e1, e2)  .Primitive("+"), seems like it worked
1 -> 3      # Error in 3 <- 1 : invalid (do_set) left-hand side to assignment
1 -> test1
print(test1) # 1, hijacking failed
`->`(1,3)   # 4, this works

With <- (or any other operator I tried), I can do it:

`<-` = `+`   
print(`<-`)
1 <- 3      # 4
1 <- test2  # Error: object 'test2' not found

rm(list=ls()) # back to sanity

So what's going on ?


Solution

  • > e <- quote(42 -> x)
    > e
    x <- 42
    

    There is only one assignment operator in R: <- (well, two: there's = but let's not complicate things). The parser interprets the symbol "->" as assignment, and creates the expression as if <- had been used.