Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
piotrklibert opened this issue Feb 13, 2018 · 0 comments
Open

Comments

@piotrklibert
Copy link
Contributor

piotrklibert commented Feb 13, 2018

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant