On 11/02/2011 06:52 AM, Albert-Jan Roskam wrote:
Hello,
I would like to overload the + operator so that it can be used to concatenate two strings, e.g
John + Doe = JohnDoe.
How can I 'unseal' the + method?
setMethod(+, signature(e1=character, e2=character), function(e1, e2) paste(e1, e2,
sep=) )
Error in setMethod(+, signature(e1 = character, e2 = character), :
the method for function + and signature e1=character, e2=character is
sealed and cannot be re-defined
Hi -- I think the two issues are that + is part of the Arith group
generic (?Methods, ?Arith) and that `+` (actually, members of the Ops
group) for primitive types dispatches directly without doing method
look-up. Personally I might
setClass(Character, contains=character)
Character - function(...) new(Character, ...)
setMethod(Arith, c(Character, Character), function(e1, e2) {
switch(.Generic,
+=Character(paste(e1, e2, sep=)),
stop(unhandled 'Arith' operator ', .Generic, '))
})
and then
Character(c(foo, bar)) + Character(baz)
[1] foobaz barbaz
Some might point to
`%+%` - function(e1, e2) paste(e1, e2, sep=)
foo %+% bar
[1] foobar
Martin
Cheers!!
Albert-Jan
~~
All right, but apart from the sanitation, the medicine, education, wine, public
order, irrigation, roads, a fresh water system, and public health, what have
the Romans ever done for us?
~~
[[alternative HTML version deleted]]
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109
Location: M1-B861
Telephone: 206 667-2793
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.