[Rd] Surprising behavior when using the reference class with the dollar symbol

Jiefei Wang @zwj|08 @end|ng |rom gm@||@com
Sun Mar 28 05:48:31 CEST 2021


Thank you for the clarification! Wow, I do not know the same rule
applies to the list object. Maybe that's because the list object will
give NULL not an error. I might have incorrectly used it in my
package.

Best,
Jiefei

On Sun, Mar 28, 2021 at 3:26 AM Bill Dunlap <williamwdunlap using gmail.com> wrote:
>
> > > It looks like when calling the dollar symbol using the function format, it
> > > treats the input argument as a character literal and does not evaluate it
> > > inside the function. I know we have the function `field` to get the slot
> > > variable, but I wonder if this is designed on purpose as the above example
> > > works for both list and S4 system.
>
> It works almost the same way with lists, the difference being that an
> unknown name with a list gives NULL and not an error.
>
> > L <- list(A=101)
> > `$`(L, A)
> [1] 101
> > `$`(L, "A")
> [1] 101
> > i <- "A"
> > `$`(L, i)
> NULL
>
> The 2nd argument is treated as a symbol and is never evaluated.  If it
> is given as a string literal then it is converted to a symbol - that
> is a holdover from the old days before backticks could be used to make
> symbol literals for symbols that included odd things like spaces and
> dollar signs.
>
> -Bill
>
> On Sat, Mar 27, 2021 at 7:49 AM Jiefei Wang <szwjf08 using gmail.com> wrote:
> >
> > Thanks, Duncan. Below is the repost of my question in plain text mode.
> >
> >
> > I'm trying to get the field value of a reference object by the field
> > name, but the dollar symbol behaves quite unusual. See example below
> >
> > .foo <- setRefClass(
> >     "foo",
> >     fields = list(
> >         a = "integer"
> >     )
> > )
> > x <- .foo$new(a=1L)
> > ## This is OK
> > x$a
> > ## This is OK
> > `$`(x, "a")
> > ## But this is not OK
> > i <- "a"
> > `$`(x, i)
> >
> > For the last line of code I get this error
> >
> > Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
> >   ‘i’ is not a valid field or method name for reference class “foo”
> >
> > It looks like when calling the dollar symbol using the function
> > format, it treats the input argument as a character literal and does
> > not evaluate it inside the function. I know we have the function
> > `field` to get the slot variable, but I wonder if this is designed on
> > purpose as the above example works for both list and S4 system.
> >
> > Best,
> > Jiefei
> >
> >
> > On Sat, Mar 27, 2021 at 10:20 PM Duncan Murdoch
> > <murdoch.duncan using gmail.com> wrote:
> > >
> > > On 27/03/2021 10:16 a.m., Jiefei Wang wrote:
> > > > Hi all,
> > > >
> > > > I'm trying to get the field value of a reference object by the field name,
> > > > but the dollar symbol behaves quite unusual. See example below
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > *.foo <- setRefClass(    "foo",    fields = list(        a = "integer"
> > > > ))x <- .foo$new(a=1L)## This is OKx$a## This is OK`$`(x, "a")## But this is
> > > > not OKi <- "a"`$`(x, i)*
> > >
> > > This is really hard to read.  Please post it again, but don't use HTML.
> > >
> > > Duncan Murdoch
> > >
> > > >
> > > > For the last line of code I get this error
> > > >
> > > >
> > > > *Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :   ‘i’ is
> > > > not a valid field or method name for reference class “foo”*
> > > >
> > > > It looks like when calling the dollar symbol using the function format, it
> > > > treats the input argument as a character literal and does not evaluate it
> > > > inside the function. I know we have the function `field` to get the slot
> > > > variable, but I wonder if this is designed on purpose as the above example
> > > > works for both list and S4 system.
> > > >
> > > > Best,
> > > > Jiefei
> > > >
> > > >       [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > R-devel using r-project.org mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-devel
> > > >
> > >
> >
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list