Surpising behavior when using an active binding as loop index in R 4.0.0

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

Surpising behavior when using an active binding as loop index in R 4.0.0

R devel mailing list
Hi,

I stumbled upon a surprising behavior when using an active binding as a
loop index variable in R 4.0.0. In contrast, the behavior observed in R
3.6.1 is in line with my expectations.

Consider the code below:

makeActiveBinding("i",
        function(value) {
                if (missing(value)) {
                        x
        } else {
                print("set")
                x <<- value
        }
        }, globalenv())

i <- 1     # output "set"
print(i)   # output [1] 1

# Surprising behavior starts here:
for(i in 2:3) print(i) # output [1] "set"
                       #        NULL
                       #        NULL

print(i)               # output NULL
print(x)               # output NULL

i <- 4                 # output "set"
print(i)               # ouput [1] 4
print(x)               # ouput [1] 4

Regards
Thomas

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

attachment0 (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Surpising behavior when using an active binding as loop index in R 4.0.0

R devel mailing list
Possibly just a symptom of the earlier behavior, but I'll amend my
example, below, with an even more disturbing observation:

Am Sat, 23 May 2020 13:19:24 +0200
schrieb Thomas Friedrichsmeier via R-devel <[hidden email]>:
[...]

> Consider the code below:
>
> makeActiveBinding("i",
> function(value) {
> if (missing(value)) {
> x
> } else {
> print("set")
> x <<- value
> }
> }, globalenv())
>
> i <- 1     # output "set"
> print(i)   # output [1] 1
>
> # Surprising behavior starts here:
> for(i in 2:3) print(i) # output [1] "set"
>                        #        NULL
>                        #        NULL
>
> print(i)               # output NULL
> print(x)               # output NULL
>
> i <- 4                 # output "set"
> print(i)               # ouput [1] 4
> print(x)               # ouput [1] 4
ls()
# Error in ls() :
#  Value of SET_STRING_ELT() must be a 'CHARSXP' not a 'NULL'

Regards
Thomas

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

attachment0 (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Surpising behavior when using an active binding as loop index in R 4.0.0

Deepayan Sarkar
A shorter reproducible example:

example(makeActiveBinding)
for (fred in 1:3) { 0 }
ls()

Both problems go away if you first do

compiler::enableJIT(2)

So looks like a bug in compiling the for loop.

-Deepayan

On Sat, May 23, 2020 at 5:45 PM Thomas Friedrichsmeier via R-devel
<[hidden email]> wrote:

>
> Possibly just a symptom of the earlier behavior, but I'll amend my
> example, below, with an even more disturbing observation:
>
> Am Sat, 23 May 2020 13:19:24 +0200
> schrieb Thomas Friedrichsmeier via R-devel <[hidden email]>:
> [...]
> > Consider the code below:
> >
> > makeActiveBinding("i",
> >       function(value) {
> >               if (missing(value)) {
> >                       x
> >       } else {
> >               print("set")
> >               x <<- value
> >       }
> >       }, globalenv())
> >
> > i <- 1     # output "set"
> > print(i)   # output [1] 1
> >
> > # Surprising behavior starts here:
> > for(i in 2:3) print(i) # output [1] "set"
> >                        #        NULL
> >                        #        NULL
> >
> > print(i)               # output NULL
> > print(x)               # output NULL
> >
> > i <- 4                 # output "set"
> > print(i)               # ouput [1] 4
> > print(x)               # ouput [1] 4
>
> ls()
> # Error in ls() :
> #  Value of SET_STRING_ELT() must be a 'CHARSXP' not a 'NULL'
>
> Regards
> Thomas
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

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

Re: [External] Re: Surpising behavior when using an active binding as loop index in R 4.0.0

luke-tierney
On Sun, 24 May 2020, Deepayan Sarkar wrote:

> A shorter reproducible example:
>
> example(makeActiveBinding)
> for (fred in 1:3) { 0 }
> ls()
>
> Both problems go away if you first do
>
> compiler::enableJIT(2)
>
> So looks like a bug in compiling the for loop.

Not in compiling but in the byte code interpreter. It was not handling
active bindings for the loop variable properly. This was fixed
yesterday in R--devel and R-patched, so will be fixed in R 4.0.1.

Best,

luke

>
> -Deepayan
>
> On Sat, May 23, 2020 at 5:45 PM Thomas Friedrichsmeier via R-devel
> <[hidden email]> wrote:
>>
>> Possibly just a symptom of the earlier behavior, but I'll amend my
>> example, below, with an even more disturbing observation:
>>
>> Am Sat, 23 May 2020 13:19:24 +0200
>> schrieb Thomas Friedrichsmeier via R-devel <[hidden email]>:
>> [...]
>>> Consider the code below:
>>>
>>> makeActiveBinding("i",
>>>       function(value) {
>>>               if (missing(value)) {
>>>                       x
>>>       } else {
>>>               print("set")
>>>               x <<- value
>>>       }
>>>       }, globalenv())
>>>
>>> i <- 1     # output "set"
>>> print(i)   # output [1] 1
>>>
>>> # Surprising behavior starts here:
>>> for(i in 2:3) print(i) # output [1] "set"
>>>                        #        NULL
>>>                        #        NULL
>>>
>>> print(i)               # output NULL
>>> print(x)               # output NULL
>>>
>>> i <- 4                 # output "set"
>>> print(i)               # ouput [1] 4
>>> print(x)               # ouput [1] 4
>>
>> ls()
>> # Error in ls() :
>> #  Value of SET_STRING_ELT() must be a 'CHARSXP' not a 'NULL'
>>
>> Regards
>> Thomas
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

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