foo <- function(x = list()) structure(x, class = "foo")
`+.foo` <- function(e1, e2) message("foo!")
bar <- function(x = list()) structure(x, class = "bar")
`+.bar` <- function(e1, e2) message("bar?")
# unaddable, no methods
list() + list()
#> Error in list() + list(): non-numeric argument to binary operator
# unaddable, first one has a method
foo() + 1
#> foo!
#> NULL
# unaddable, second one has a method
1 + foo()
#> foo!
#> NULL
# unaddable, both have methods
foo() + bar()
#> Warning: Incompatible methods ("+.foo", "+.bar") for "+"
#> Error in foo() + bar(): non-numeric argument to binary operator
# addable, both have methods
foo(1) + bar(1)
#> Warning: Incompatible methods ("+.foo", "+.bar") for "+"
#> [1] 2
#> attr(,"class")
#> [1] "foo"Created on 2018-09-17 by the reprex package (v0.2.0).
do_arith().https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L386
DispatchGroup().https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L403-L404
https://github.com/wch/r-source/blob/fabab8108783c8c48ae277120cfa0046b7b48f17/src/main/eval.c#L3541
https://github.com/wch/r-source/blob/fabab8108783c8c48ae277120cfa0046b7b48f17/src/main/eval.c#L3563-L3575
https://github.com/wch/r-source/blob/fabab8108783c8c48ae277120cfa0046b7b48f17/src/main/eval.c#L3606-L3614
R_binary(). (If either one is a scalar, there's a shortcut, but I omit the detail here.)https://github.com/wch/r-source/blob/fabab8108783c8c48ae277120cfa0046b7b48f17/src/main/eval.c#L3618-L3621
R_binary(): https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L493-L494R_binary()with a warning. (There's some extra step fordifftime, but I omit the detail here.)https://github.com/wch/r-source/blob/fabab8108783c8c48ae277120cfa0046b7b48f17/src/main/eval.c#L3623-L3641
https://github.com/wch/r-source/blob/fabab8108783c8c48ae277120cfa0046b7b48f17/src/main/eval.c#L3642-L3649 (See
lsxpandlmethare used in the following lines)R_binary()https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L518
NILSXP,CPLXSXP,REALSXP,INTSXP, orLGLSXP, raise an error.https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L529-L530
FIXUP_NULL_AND_CHECK_TYPES()https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L510-L516complex_binary()orreal_binary()orinteger_binary(). (There's a lot of special treatments on ts and array, but I omit the details here)https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/main/arithmetic.c#L648-L673