From h.wickham at gmail.com  Sat Nov  1 06:08:27 2014
From: h.wickham at gmail.com (Hadley Wickham)
Date: Sat, 1 Nov 2014 00:08:27 -0500
Subject: [Rd] ScalarLogical and setAttrib
In-Reply-To: <CABFfbXvzf4QV6Tb5qi53ddJh_MEhJEDeb3aFwL=s-Nnnpe2okA@mail.gmail.com>
References: <CABFfbXvzf4QV6Tb5qi53ddJh_MEhJEDeb3aFwL=s-Nnnpe2okA@mail.gmail.com>
Message-ID: <CABdHhvFnLEbhK1NbAGY0gFc77G_dYDGSUXZROCA9RwZroLiRNg@mail.gmail.com>

I believe this is by design (and changed relatively recently). FALSE and
TRUE are singletons, like NULL.

Hadley.

On Friday, October 31, 2014, Jeroen Ooms <jeroen.ooms at stat.ucla.edu> wrote:

> Is it expected that attributes set on a LGLSXP created by
> ScalarLogical will apply to all future objects created by
> ScalarLogical as well? For example: the 'test1' function below returns
> FALSE and 'test2' returns FALSE with an attribute:
>
>   library(inline)
>   test1 <- cfunction(body = 'return ScalarLogical(0);')
>   test2 <- cfunction(body = '
>     SEXP success = PROTECT(ScalarLogical(0));
>     setAttrib(success, install("foo"), mkString("bar"));
>     UNPROTECT(1);
>     return success;
>   ')
>
> However after running test2(), then test1() will also return the attribute:
>
>   > test1()
>   [1] FALSE
>   > test2()
>   [1] FALSE
>   attr(,"foo")
>   [1] "bar"
>   > test1()
>   [1] FALSE
>   attr(,"foo")
>   [1] "bar"
>
> It seems like ScalarLogical returns a singleton object, which is not
> the case for ScalarInteger or ScalarReal. I am currently working
> around this using duplicate(ScalarLogical(0)), but was quite surprised
> by this behavior of ScalarLogical.
>
> ______________________________________________
> R-devel at r-project.org <javascript:;> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>


-- 
http://had.co.nz/

	[[alternative HTML version deleted]]



[Rd] ScalarLogical and setAttrib
Hadley Wickham