Giter Site home page Giter Site logo

`do` scope about imba HOT 7 CLOSED

imba avatar imba commented on May 8, 2024
`do` scope

from imba.

Comments (7)

 avatar commented on May 8, 2024

or at least allow this construction to work:

post data, {
  some code
}.bind(this)

actually it throws: Module build failed: Parse error at [981:1]: Unexpected '.'

from imba.

 avatar commented on May 8, 2024

err, i meant

post data, { |response|
  some code
}.bind(this)

from imba.

somebee avatar somebee commented on May 8, 2024

We do consider adding a func-keyword (or similar) - that behaves exactly like do but without binding self (fat arrow). It's not totally clear to me what you are trying to achieve -- but you can use this in imba as well. this will always refer to the real this, just like in plain javascript. You can also export declared methods with def in imba. These are closed scopes (self is not brought in from outer scope). So if I understand you correctly, you might want to do:

export def save data
    post(data) do console.log('something')

This compiles to:

function save(data){
    return this.post(data,function() { return console.log('something'); });
}; exports.save = save;

from imba.

somebee avatar somebee commented on May 8, 2024

I've promised to release the new site (with docs) this week, and we'll explain it there in more detail. But yes, lambda-style blocks are 'open', where their implicit self refers to the outer 'closed' scope. You can still use this explicitly though, it will never refer to anything else than the actual js this.

Method declarations (def) are 'closed' scopes, where the implicit self is not brought in from outer scopes.
And some more undocumented syntax... As you mentioned:

post data, { |response| some code }.bind(this)

is not valid in imba, we use regular parens instead:

post data, (|response| 
   some code
).bind(this)

This only works for methods with arguments though, so it is a rather clumsy syntax which might be deprecated in favour of the ruby-like syntax you wrote. To be on the safe side, you could just do this:

post data, (do
   some code
).bind(this)

# with parens
post data, (do |res|
   some code
).bind(this)

from imba.

 avatar commented on May 8, 2024

exactly, but inside function() { return console.log('something'); } the this will be Window,
that's the issue.

my idea was do to generate functions like this:

function() { return console.log('something'); }.bind(this)

from imba.

somebee avatar somebee commented on May 8, 2024

The implicit self of a do block is always the closest 'closed' scope, not the outermost. def save has a closed scope, which the inner do will refer to. So in your example this will not be the window.

export def save data
    post(data) do
        self # self here is the method on which save is called

compiles to

function save(data){
    var self = this;
    return this.post(data,function() {
        return self; // self here is the context on which save is called
    });
}; exports.save = save;

This is practically the same as:

function save(data){
    return this.post(data,function() {
        return this; // self here is the context on which save is called
    }.bind(this));
}; exports.save = save;

Except we get better performance, and we can still use the actual this in addition to the implied self.
Your idea is good, and it is essentially what we do, except we don't use bind (mostly for performance reasons), we cache a reference to bind instead. One confusing thing about our examples is that we use console, which is a globally exported variable, so it does (as you can see) not compile to self.console.log(...)

from imba.

 avatar commented on May 8, 2024

ok, wonderful,
export def works perfect!
self is just what it is expected to be.
thank you.

Imba is impressive!
hoping it is here to stay.
keep doing marketing.
nowadays marketing is most important language feature :)

from imba.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.