Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
method-dispatch instead (and encompassing) mode-methods
git-svn-id: https://svn.r-project.org/R/trunk@8504 00db46b3-68df-0310-9c12-caf00c1e9a41
- Loading branch information
maechler
committed
Mar 13, 2000
1 parent
296fd5e
commit 9bd0178
Showing
4 changed files
with
107 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#### Testing UseMethod() and even more NextMethod() | ||
#### | ||
|
||
###-- Group methods | ||
|
||
### Arithmetic "Ops" : | ||
">.bar" <- function(...) print(">.bar") | ||
">.foo" <- function(...) print(">.foo") | ||
Ops.foo <- function(...) { | ||
print("Ops.foo") | ||
NextMethod() | ||
} | ||
Ops.bar <- function(...) | ||
print("Ops.bar") | ||
|
||
x <- 2:4 ; class(x) <- c("foo", "bar") | ||
y <- 4:2 ; class(y) <- c("bar", "foo") | ||
|
||
## The next 4 give a warning each about incompatible methods: | ||
x > y | ||
y < x # should be the same (warning msg not, however) | ||
x == y | ||
x <= y | ||
|
||
x > 3 ##[1] ">.foo" | ||
|
||
rm(list=">.foo") | ||
x > 3 #-> "Ops.foo" and ">.bar" | ||
|
||
|
||
|
||
### ------------ was ./mode-methods.R till R ver. 1.0.x ---------------- | ||
|
||
###-- Using Method Dispatch on "mode" etc : | ||
|
||
abc <- function(x, ...) { | ||
if (is.null(class(x))) class(x) <- data.class(x) | ||
cat("abc: Before dispatching; x="); str(x) | ||
UseMethod("abc", x,...) ## UseMethod("abc") (as in S) fails | ||
} | ||
|
||
abc.default <- function(x, ...) sys.call() | ||
|
||
"abc.(" <- function(x) | ||
cat("'(' method of abc:", deparse(sys.call(sys.parent())),"\n") | ||
abc.expression <- function(x) | ||
cat("'expression' method of abc:", deparse(sys.call(sys.parent())),"\n") | ||
|
||
abc(1) | ||
e0 <- expression((x)) | ||
e1 <- expression(sin(x)) | ||
abc(e0) | ||
abc(e1) | ||
abc(e0[[1]]) | ||
abc(e1[[1]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.