From murdoch at stats.uwo.ca  Mon Oct  2 21:45:23 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Mon, 02 Oct 2006 15:45:23 -0400
Subject: [Rd] More strange [[ behaviour
In-Reply-To: <452166BB.3040901@fhcrc.org>
References: <452166BB.3040901@fhcrc.org>
Message-ID: <45216C53.4040803@stats.uwo.ca>

On 10/2/2006 3:21 PM, Herve Pages wrote:
> Hi,
> 
>> setClass("MyList", "list")
> [1] "MyList"
>> a <- new("MyList")
>> a
> An object of class "MyList"
> list()
>> setMethod("[[", "MyList", function(x, i, j, ...) cat("Just testing\n"))
>> a[[]]
> Just testing
>> a[[1]]
> Just testing
>> a[[a=1]]
> Just testing
>> a[[b=1]]
> Just testing
> ...
>> a[[v=1]]
> Just testing
>> a[[w=1]]
> Just testing
>> a[[x=1]]
> Error in a[[x = 1]] : subscript out of bounds
>> a[[y=1]]
> Just testing
>> a[[z=1]]
> Just testing
> 
> Can someone explain me why the "[[x=1]]" case is treated differently?

Indexing is a function call, with arguments x, i, j, ... .  If you use 
y=1, you're setting something in the "..." part of the arg list.  If you 
say x=1, you're setting the first arg, but because a[[x=1]] is the same 
as "[["(a, x=1), it is going to evaluate it as "[["(x=1, i=a) and try to 
index 1 by a instead of a by 1.

Duncan Murdoch




[Rd] More strange [[ behaviour
Duncan Murdoch