How does scope work in Io?

Dan Prince

I'm not quite sure how variable scope works in Io. The docs say it has closures, but I don't seem to be able to see idx from within the next and prev methods. Parent visibility is the key premise of closures, so how can they work?

List iterator := method(
    idx := 0

    itr := Object clone
    itr next := method(
        idx = idx + 1
        return self at(idx)
    )

    itr prev := method(
        idx = idx - 1
        return self at(idx)
    ) 

    return itr
)

How should this be achieved?

jer

So you're fundamentally misunderstanding how methods and blocks work, but that's ok. Let's go over the basics:

  1. Methods are blocks who activate when you call them by name, and whose scope is set to nil. When we talk about scope.
  2. Blocks have their scope set to the context in which they're created.

context means a locals object, basically a stack frame. Scope means who will be the "sender" of the block activation when the block/method is invoked. You can access this by the call sender object inside the context of a method or block.

Now, let's look at your code. It's almost perfect, there's only one thing missing, and it's non-obvious.

Since methods have dynamic scope, their scope message returns nil. This signifies to the evaluator that whichever object received that message, should be passed in as the sending context. We don't want that behaviour, we want to capture some scope, specifically the locals of the iter method we've defined. Let's look at a corrected example:

List iterator := method(
    idx := 0

    itr := Object clone
    itr next := method(
        idx = idx + 1
        at(idx)
    ) setScope(thisContext)

    itr prev := method(
        idx = idx - 1
        at(idx)
    ) setScope(thisContext)

    itr
)

I've simplified the bodies, but they haven't changed in terms of functionality (apart from a few less message sends). The important thing is the setScope call passed to the method before assignment to next/prev. I could have chose to rewrite this as:

iter prev := block(
    idx = idx - 1
    at(idx)
) setIsActivatable(true)

but I'd have then had to make the block activatable, since blocks are not activatable by default. The code above, and the corrected itr prev using method() are functionally equivalent.

Methods are not closures, blocks are. A block is simply a method whose scope is non-nil, they are the same object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does scope work in Ruby?

From Dev

What is the skimlinks scope and how does it work

From Java

How does the scope of variables in julia modules work?

From Dev

How does this hoisting work with block scope?

From Dev

How does this hoisting work with block scope?

From Dev

How Does Spring Batch Step Scope Work

From Dev

What is the skimlinks scope and how does it work

From Dev

How does $.ajax success scope work?

From Dev

How does $mdDialog work with promise and $scope?

From Dev

how does jsp:useBean scope attribute work?

From Dev

How does fallback work with socket.io?

From Dev

Angular $scope does not work

From Dev

Angular $scope does not work

From Dev

Why does it work with $scope but not with `this`?

From Dev

How does telepat.io work? Architecture of telepat.io

From Dev

$scope.formName.$setPristine() does not work ; How to reset form in angular?

From Dev

How does $scope.apply() work exactly in AngularJS?

From Dev

how does $scope in controllers work and different ways of declaring controllers?

From Dev

What is the scope of the Single Responsibility Principle and how does it work with DRY?

From Dev

How does the session scope of a bean work in a Spring MVC application?

From Dev

How does variable scope work within the Mocha test framework?

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

Function declared outside class scope but not friend. How does this work?

From Dev

how does scope work when using property in Python 3?

From Dev

Why does the scope work like this?

From Dev

How does card.io image processing work?

From Dev

How does IO monad work in System.Random

From Dev

Why does $scope.$watch work but $scope.$watchCollection does not?

Related Related

  1. 1

    How does scope work in Ruby?

  2. 2

    What is the skimlinks scope and how does it work

  3. 3

    How does the scope of variables in julia modules work?

  4. 4

    How does this hoisting work with block scope?

  5. 5

    How does this hoisting work with block scope?

  6. 6

    How Does Spring Batch Step Scope Work

  7. 7

    What is the skimlinks scope and how does it work

  8. 8

    How does $.ajax success scope work?

  9. 9

    How does $mdDialog work with promise and $scope?

  10. 10

    how does jsp:useBean scope attribute work?

  11. 11

    How does fallback work with socket.io?

  12. 12

    Angular $scope does not work

  13. 13

    Angular $scope does not work

  14. 14

    Why does it work with $scope but not with `this`?

  15. 15

    How does telepat.io work? Architecture of telepat.io

  16. 16

    $scope.formName.$setPristine() does not work ; How to reset form in angular?

  17. 17

    How does $scope.apply() work exactly in AngularJS?

  18. 18

    how does $scope in controllers work and different ways of declaring controllers?

  19. 19

    What is the scope of the Single Responsibility Principle and how does it work with DRY?

  20. 20

    How does the session scope of a bean work in a Spring MVC application?

  21. 21

    How does variable scope work within the Mocha test framework?

  22. 22

    Prolog: how does variable instantiating work, what is the scope of local variables

  23. 23

    Prolog: how does variable instantiating work, what is the scope of local variables

  24. 24

    Function declared outside class scope but not friend. How does this work?

  25. 25

    how does scope work when using property in Python 3?

  26. 26

    Why does the scope work like this?

  27. 27

    How does card.io image processing work?

  28. 28

    How does IO monad work in System.Random

  29. 29

    Why does $scope.$watch work but $scope.$watchCollection does not?

HotTag

Archive