setClass accepts slot-mismatch between slots and prototype arguments

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

setClass accepts slot-mismatch between slots and prototype arguments

R devel mailing list
I was installing the 'diffobj' package into TERR and got an error from the
call
StyleSummary <- setClass("StyleSummary",
  slots=c(container="ANY", body="ANY", map="ANY"),
  prototype=list(
    container=function(x) sprintf("\n%s\n", paste0(x, collapse="")),
    body=identity,
    detail=function(x) sprintf("\n%s\n", paste0("  ", x, collapse="")),
    map=function(x) sprintf("\n%s", paste0("  ", x, collapse="\n"))
  ))
because the prototype contained components not in the slots list.  R does
not complain about the mismatch, but new("StyleSummary") does name make
something with a 'detail' slot.  Should this be an error?

I suspect that the package writer intended to include 'detail' in the slots
argument.

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: setClass accepts slot-mismatch between slots and prototype arguments

R devel mailing list
Indeed that was on oversight on my part.  It is surprising that things like this work:

> setClass('test', slots=c(a='ANY'), prototype=list(a=NULL, b='hello'))
> new('test')@b
[1] "hello"
> slotNames(new('test'))
[1] "a"

I'm planning a release of diffobj right now so I will fix this, but I agree that R should probably throw an error here.

Best,

B.




On Thursday, January 10, 2019, 11:30:13 AM EST, William Dunlap <[hidden email]> wrote:





I was installing the 'diffobj' package into TERR and got an error from the call
StyleSummary <- setClass("StyleSummary",
  slots=c(container="ANY", body="ANY", map="ANY"),
  prototype=list(
    container=function(x) sprintf("\n%s\n", paste0(x, collapse="")),
    body=identity,
    detail=function(x) sprintf("\n%s\n", paste0("  ", x, collapse="")),
    map=function(x) sprintf("\n%s", paste0("  ", x, collapse="\n"))
  ))
because the prototype contained components not in the slots list.  R does not complain about the mismatch, but new("StyleSummary") does name make something with a 'detail' slot.  Should this be an error?

I suspect that the package writer intended to include 'detail' in the slots argument.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

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

Re: setClass accepts slot-mismatch between slots and prototype arguments

R devel mailing list
In reply to this post by R devel mailing list
Thanks for the report. There is a comment from 2001 in the header for
reconcilePropertiesAndPrototype() that states:

"The prototype may imply slots not in the properties list.  It is not
required that the extends classes be define at this time.  Should it
be?"

But somewhere in the mid 2000's, I think, it became necessary for the
extends classes to be defined, so it may be safe to add this
constraint.

I'll commit a fix soon. While fixing, I found that the type
consistency check for slots shared with superclasses was completely
broken ever since it was written in 2002.

Michael

On Thu, Jan 10, 2019 at 8:31 AM William Dunlap via R-devel
<[hidden email]> wrote:

>
> I was installing the 'diffobj' package into TERR and got an error from the
> call
> StyleSummary <- setClass("StyleSummary",
>   slots=c(container="ANY", body="ANY", map="ANY"),
>   prototype=list(
>     container=function(x) sprintf("\n%s\n", paste0(x, collapse="")),
>     body=identity,
>     detail=function(x) sprintf("\n%s\n", paste0("  ", x, collapse="")),
>     map=function(x) sprintf("\n%s", paste0("  ", x, collapse="\n"))
>   ))
> because the prototype contained components not in the slots list.  R does
> not complain about the mismatch, but new("StyleSummary") does name make
> something with a 'detail' slot.  Should this be an error?
>
> I suspect that the package writer intended to include 'detail' in the slots
> argument.
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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