3

Why does this work

print.character = function(x) cat("Use cat instead!", x)
print("tre")
# Use cat instead! tre

but this throws an error?

"+.character" = function(lhs, rhs) paste0(lhs, " + ", rhs)
"tre" + "tri"

Error in "tre" + "tri" : non-numeric argument to binary operator

jakub
  • 4,774
  • 4
  • 29
  • 46
  • 2
    I think because '+' is a binary operator it messes up s3 method dispatch. See here http://stackoverflow.com/questions/4730551/making-a-string-concatenation-operator-in-r – Tom Liptrot Nov 07 '16 at 14:26
  • Additionally, `"tre"` and `"tri"` are implicitly "class"ed as "character" -- i.e. the do not have a set "class" attribute. Setting it explicitly (`structure("tre", class = "character") + structure("tri", class = "character")`) or, better, defining another more straigthforward "class" should work. – alexis_laz Nov 07 '16 at 15:14

0 Answers0