Should slot<-() alter its first argument?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Should slot<-() alter its first argument?

R devel mailing list
We noticed that the slot<- function alters its first argument, which goes
against the grain of a functional language.  The similar @<- does not
change its first argument.  Is this intended?  The timeSeries and distr
package depend on this altering.

> setClass("Z", rep=representation(x="character"))
> z <- new("Z", x="orig")
> `@<-`(z, "x", value="newer")
An object of class "Z"
Slot "x":
[1] "newer"

> z
An object of class "Z"
Slot "x":
[1] "orig"

>
> `slot<-`(z, "x", value="newest")
An object of class "Z"
Slot "x":
[1] "newest"

> z
An object of class "Z"
Slot "x":
[1] "newest"

Bill Dunlap
TIBCO Software
wdunlap tibco.com

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Should slot<-() alter its first argument?

Benjamin Tyner
It appears this started with R version 3.5.0. Under R 3.4.4 we have:

 > setClass("Z", rep=representation(x="character"))
 > z <- new("Z", x="orig")
 > `@<-`(z, "x", value="newer")
An object of class "Z"
Slot "x":
[1] "newer"

 > z
An object of class "Z"
Slot "x":
[1] "newer"

 > `slot<-`(z, "x", value="newest")
An object of class "Z"
Slot "x":
[1] "newest"

 > z
An object of class "Z"
Slot "x":
[1] "newest"


> We noticed that the slot<- function alters its first argument, which goes
> against the grain of a functional language.  The similar @<- does not
> change its first argument.  Is this intended?  The timeSeries and distr
> package depend on this altering.
>
> >/setClass("Z", rep=representation(x="character")) />/z <- new("Z", x="orig") />/`@<-`(z, "x", value="newer") /An object of class "Z"
> Slot "x":
> [1] "newer"
>
> >/z /An object of class "Z"
> Slot "x":
> [1] "orig"
>
> >//>/`slot<-`(z, "x", value="newest") /An object of class "Z"
> Slot "x":
> [1] "newest"
>
> >/z /An object of class "Z"
> Slot "x":
> [1] "newest"
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Should slot<-() alter its first argument?

R devel mailing list
In reply to this post by R devel mailing list
The core of Bioconductor and the methods package itself once took
advantage of this "feature" to avoid unnecessary duplication. Since
the introduction of shallow copying, those abuses have been removed.

Note that these assignment functions always have issues due to
optimizations that assume <-() is called. For example,

>  setClass("Z", rep=representation(x="character"))
> z <- new("Z", x="orig")

> z2 <- `@<-`(z, "x", value="newer")
> .Internal(inspect(z2)) # NAMED==1, as it should be
@565446e0bdf8 25 S4SXP g0c0 [OBJ,NAM(1),S4,gp=0x10,ATT]

> `@<-`(z2, "x", value="newest")
An object of class "Z"
Slot "x":
[1] "newest"
> z2 # modified in place due to NAMED==1, because we assume assignment back to itself
An object of class "Z"
Slot "x":
[1] "newest"

Since we assume <-() bumps NAMED, z2 has its NAMED cleared even though
it's obviously named:
> z2 <- `@<-`(z, "x", value="newer")
> .Internal(inspect(z2))
@56544726ae60 25 S4SXP g0c0 [OBJ,NAM(1),S4,gp=0x10,ATT]
> .Internal(inspect(`@<-`(z2, "x", value="newest"))) # NAMED == 0, same address as above
@56544726ae60 25 S4SXP g0c0 [OBJ,S4,gp=0x10,ATT]

So I guess we could make slot<-() a bit safer but unless we give up
the optimizations or maybe inform the "gets" functions that they are
being called outside of complex assignment, there will be exploits.

Michael

On Thu, Sep 19, 2019 at 11:19 AM William Dunlap via R-devel
<[hidden email]> wrote:

>
> We noticed that the slot<- function alters its first argument, which goes
> against the grain of a functional language.  The similar @<- does not
> change its first argument.  Is this intended?  The timeSeries and distr
> package depend on this altering.
>
> > setClass("Z", rep=representation(x="character"))
> > z <- new("Z", x="orig")
> > `@<-`(z, "x", value="newer")
> An object of class "Z"
> Slot "x":
> [1] "newer"
>
> > z
> An object of class "Z"
> Slot "x":
> [1] "orig"
>
> >
> > `slot<-`(z, "x", value="newest")
> An object of class "Z"
> Slot "x":
> [1] "newest"
>
> > z
> An object of class "Z"
> Slot "x":
> [1] "newest"
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



--
Michael Lawrence
Scientist, Bioinformatics and Computational Biology
Genentech, A Member of the Roche Group
Office +1 (650) 225-7760
[hidden email]

Join Genentech on LinkedIn | Twitter | Facebook | Instagram | YouTube

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel