Basically, through overloading of operators you can get any behaviour you want. With the following code the brackets are overloaded to behave the way you want them to behave:
# overload the brackets for reading out
"[.pmatch_vec" = function(obj,idx){
origclass <- setdiff(class(obj),"pmatch_vec")
if (length(origclass)==0) origclass <- ""
class(obj) <- origclass
if (!is.character(idx))
return(obj[idx])
else
return(obj[grep(paste("^",idx,sep=""),names(obj))])
}
# overload the assignment operator []<-
"[<-.pmatch_vec" = function(obj,idx,value){
saveclass <- class(obj)
origclass <- setdiff(class(obj),"pmatch_vec")
if (length(origclass)==0) origclass <- ""
class(obj) <- origclass
if (!is.character(idx))
obj[idx] <- value
else
obj[grep(paste("^",idx,sep=""),names(obj))] <- value
class(obj) <- saveclass
return(obj)
}
Since it is dangerous to overload the brackets in general, they were overloaded only for the defined class "pmatch_vec". Additionally note, that within these functions "pmatch_vec" is temporarily removed from the class attribute in order to avoid infinite recursion.
Here are some examples for the behaviour of atomic vectors defined to be of class "pmatch_vec":
# create some vector
A = 1:6
names(A) <- c(paste("a",1:3,sep=""),paste("b",1:3,sep=""))
# set the class
class(A) = c("pmatch_vec")
# some demonstraton
A["a"]
# a1 a2 a3
# 1 2 3
A["b"]
# b1 b2 b3
# 4 5 6
A["b"] <- 7
A
# a1 a2 a3 b1 b2 b3
# 1 2 3 7 7 7
If the vector used for indexing is not of type character, the class "pmatch_vec" behaves as if it is an ordinary atomic vector:
A[1:2] <- 8
A[1:4]
# a1 a2 a3 b1
# 8 8 3 7