Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

about Delegation

Last post 01-16-2010, 4:13 AM by ialex329. 6 replies.
Sort Posts: Previous Next
  •  01-09-2010, 1:09 AM 3607

    about Delegation

    Hi all,
    I'm a newbie of using squirrel script. It's a powerful script lang. I like it very much. But I have a problem about delegation introduced in the document.

    // original code snippet in the doc
    Entity <- {}

    function Entity::DoStuff()
    {
        ::print(_name);
    }

    local newentity = {
        _name="I’m the new entity"
    }
    newentity.setdelegate(Entity)
    newentity.DoStuff();

    It will print “I’m the new entity”, but when I assign a new variable also named "_name", it just does not work as expected.
    local _name = "outside scope"

    Entity <- {}
    function Entity::DoStuff()
    {
        ::print(_name);
    }

    local newentity = {
        _name="I’m the new entity"
    }
    newentity.setdelegate(Entity)
    newentity.DoStuff();
    is there anything wrong about it? or I just misunderstand about "Delegation" in squirrel? any suggestions about how to write it correctly in this situation?

    Thanks Big Smile [:D]

  •  01-12-2010, 9:34 PM 3609 in reply to 3607

    Re: about Delegation

    this seems to work (on 3.0 beta 1)

    local _name = "outside scope"

    Entity <- {}
    function Entity::DoStuff()
    {
        ::print(this._name);
    }

    local newentity = {
        _name="I’m the new entity"
    }
    newentity.setdelegate(Entity)
    newentity.DoStuff();

    but the manual does not explain why local variables take precedence over the table slots?
  •  01-12-2010, 9:55 PM 3610 in reply to 3609

    Re: about Delegation

    OK the manual does say

    "Squirrel first checks if an identifier is a local variable (function arguments are local variables) if not it checks if it is a member of the environment object (this). "

    but do "local variables" include these in the outer scopes?
  •  01-14-2010, 11:46 AM 3612 in reply to 3610

    Re: about Delegation

    Hope Alberto can respond to this...


    seems the reasonable way to determine the variable scope should be


    in terms of priority,

    local variables of the current block
    function parameters
    slots of "this" (the current environment)
    local variables of the outter scopes


    else it may be better just to demand that, in a function body,

    all access to slots of the current environment must be prefixed with "this."
    and all global variables must be prefixed with "::"

    if that helps with performance people may be willing to accept?
  •  01-15-2010, 2:20 AM 3613 in reply to 3612

    Re: about Delegation

    the look up of a symbol is in this order

    1)local variables (function parameters are local variables)
    2)free variables(local from outer scope)
    3)constants
    4)slot in the 'this' object
    5)if 4 fails at runtime it falls back the root table

    if the symbol is prefixed with :: then it goes straight to the root table

    if the symbol is prefixed with 'this.' then it goes straight to the this slots

    Alberto
  •  01-15-2010, 11:23 AM 3615 in reply to 3613

    Re: about Delegation

    I guess some people may expect Step 4 go to between Step 1 and Step 2...

    That seems to me to be more natural...

    Of course based on your description the explict prefixes should be faster and may be a reason for people to use them.
  •  01-16-2010, 4:13 AM 3617 in reply to 3615

    Re: about Delegation

    It works. Thank you, Alberto and atai.
     it's reasonable to use the explicit prefixes. and now I understand why the examples in the document usually use explicit prefixes. I like the style of squirrel.
    Thanks for replying and detailed explanation.

    Andrews.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems