Skip to content
Prev 61442 / 63421 Next

Multiple Assignment built into the R Interpreter?

On 11/03/2023 11:57 a.m., Ivan Krylov wrote:
If I were doing it, here's what I'd propose:

   '[' formlist ']' LEFT_ASSIGN expr
   '[' formlist ']' EQ_ASSIGN expr
   expr RIGHT_ASSIGN  '[' formlist ']'

where `formlist` has the syntax of the formals list for a function 
definition.  This would have the following semantics:

    {
      *tmp* <- expr

      # For arguments with no "default" expression,

      argname1 <- *tmp*[[1]]
      argname2 <- *tmp*[[2]]
      ...

      # For arguments with a default listed

      argname3 <- with(*tmp*, default3)
    }


The value of the whole thing would therefore be (invisibly) the value of 
the last item in the assignment.

Two examples:

   [A, B, C] <- expr   # assign the first three elements of expr to A, 
B, and C

   [A, B, C = a + b] <- expr  # assign the first two elements of expr
                              # to A and B,
                              # assign with(expr, a + b) to C.

Unfortunately, I don't think this could be done entirely by transforming 
the expression (which is the way |> was done), and that makes it a lot 
harder to write and to reason about.  E.g. what does this do?

   A <- 0
   [A, B = A + 10] <- list(1, A = 2)

According to the recipe above, I think it sets A to 1 and B to 12, but 
maybe a user would expect B to be 10 or 11.  And according to that 
recipe this is an error:

   [A, B = A + 10] <- c(1, A = 2)

which probably isn't what a user would expect, given that this is fine:

   [A, B] <- c(1, 2)

Duncan Murdoch

Thread (22 messages)

Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Ivan Krylov Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Gabriel Becker Multiple Assignment built into the R Interpreter? Mar 11 Kevin Ushey Multiple Assignment built into the R Interpreter? Mar 11 @vi@e@gross m@iii@g oii gm@ii@com Multiple Assignment built into the R Interpreter? Mar 11 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 12 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 12 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 12 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 12 Pavel Krivitsky Multiple Assignment built into the R Interpreter? Mar 12 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 13 Gabor Grothendieck Multiple Assignment built into the R Interpreter? Mar 13 Martin Maechler Multiple Assignment built into the R Interpreter? Mar 14 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 14