Skip to content

Allow assign operators to work for arbitrary messages on the left side #395

Open
@piotrklibert

Description

@piotrklibert

Hi,

while I was working on PyParsing port for Io, I hit a problem with assignment operators, namely that they require their left operand to be a simple name. This is enforced during "operator shuffling" and is implemented in IoMessage_opShuffle.c.

After some wrangling with the implementation, I managed to create a patch. I opened a PR with it here: #394

Additionally, I wrote a blog post about the whole thing, posted here: https://klibert.pl/posts/adding_destructuring_bind_to_io.html - I plan to post it to news.ycombinator.com and maybe some other sites next week, and I'd appreciate any hints on how to make the post - and, of course, the PR - better.

TL;DR: my patch makes this Io code possible and working:

Object destructure := method(
  # target [a, b] <- list(9,8) becomes: target a := 9; target b := 8
  msg := call argAt(0)
  lst := call evalArgAt(1)
  target := call target
  msg arguments foreach(i, x,
      target setSlot(x name, lst at(i))
  )
  target
)
# inform the parser about our new operator
OperatorTable addAssignOperator("<-", "destructure")


o := Object clone
o [wa, wb, x] <- list(3, 123)
o println
# prints:
#  Object_0x19bcb60:
#  wa               = 3
#  wb               = 123
#  x                = nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions