Not. R allows only
- override existing operators (e.g.
+ or, indeed, <- ), - define new infix operators, surrounding them with
%β¦% .
These are the rules we must play. However, inside these rules everything is fair and games. For example, we can override + for character strings to perform concatenation without destroying its normal value (addition):
> `+` function (e1, e2) .Primitive("+")
This is the old definition that we want to keep for numbers:
> `+.default` = .Primitive('+') > `+.character` = paste0 > `+` = function (e1, e2) UseMethod('+') > 1 + 2 [1] 3 > 'hello' + 'world' [1] "helloworld"
This uses the S3 class system in R to do + generic as its first argument.
The list of operators that can be overridden is quite eclectic. At the first count, it contains the following operators:
+ , - , * , / , ^ , ** , & , | , : , :: , ::: , $ , $<- , = , <- , <<- , == , < , <= , > , >= != , ~ , && , || ! , ? , ?? , @ , @<-
(taken from the source code of the modules .)
Alternatively, you can override -> by overriding <- . It seems like this rarely makes sense - but at least one good example of this exists to define less verbose lambdas:
> sapply(1 : 4, x -> 2 * x) [1] 2 4 6 8
Implementation as an entity