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

Valid value types - resolving promise with nil #3

Open
4O4 opened this issue May 4, 2017 · 7 comments
Open

Valid value types - resolving promise with nil #3

4O4 opened this issue May 4, 2017 · 7 comments

Comments

@4O4
Copy link

4O4 commented May 4, 2017

README states, that:

  • Promises cannot be resolved with null (nil). Lua does not distinguish between

    function()
        return nil
    end

    and

    function()
        return
    end
  • Lua does not have an undefined type.

Can you provide a further explanation why promises cannot be resolved with nil in this implementation? Because I believe that the fact that lua does not distinguish between those two return variants doesn't make any difference here and doesn't justify this behaviour.

In javascript, return; or return undefined; or no return statement at all, have the same effect (function result is undefined) and it is allowed to resolve a promise without value / with undefined.

It's pretty annoying that promise.lua doesn't allow this and you are forced to explicitly provide dummy value just to make the resolve() work. It seems that nil is banned in https://github.com/Billiam/promise.lua/blob/master/promise.lua#L53 and removing this condition wouldn't break anything. But... It's possible that I'm simply wrong here, so I would really like to get more information about this.

@Luca-spopo
Copy link

I'll just leave an observation:

function()
    return nil
end

and

function()
    return
end

can be distinguished using

local functionInQuestion //Lets assume its defined as one of the above
print(select("#", functionInQuestion())==1)

I do not know what changes you would have to make to the code to implement this, but distinguishing them is certainly possible.

@4O4
Copy link
Author

4O4 commented Jan 29, 2018

@Luca-spopo nice trick! Although I still believe that this distinction is not needed in promise.lua and resolving with nil is absolutely fine and doesn't cause any strange side effects. Maybe someone will prove me wrong on this in the future but for now I accept my own thesis as a fact 😄

@jojo59516
Copy link

jojo59516 commented Nov 22, 2019

@4O4 I agree with you.
In my opinion, "a promise has no value" and "a promise has a nil value" are the same in Lua.
After all, "t[k] = nil" means "remove key k from table t" in Lua.

@Luca-spopo
Copy link

Luca-spopo commented Nov 22, 2019

@jojo59516 it's true that lua tables can't store nil, but the functions can return them.

return
return nil
return nil, nil

are all distinct in lua and, depending on who's coding, the difference can matter

I agree with 404 that changes need to be made, but it's because the doc is wrong in asserting that the functions cannot be distinguished

@jojo59516
Copy link

jojo59516 commented Nov 22, 2019

@Luca-spopo Yes, I know they are actually different.
But the reason of 1.3 in README, if I am not mistaken, seems to be that:

  • promise.lua cannot distinguish between "no value (or undefined in javascript)" and "nil", returned by callbacks passed to promise:next() method. (Refer to line 155 in promise.lua.)
  • On the other hand, promise.lua stores the returned value (by the callbacks, mentioned above) to promise.value. (Refer to line 59 in promise.lua.)

So, storing a nil value is same as storing no value in Lua. Or we can consider that a promise always has a value, and it is default to be nil.

(Forgive my bad English :)

@ikegami
Copy link

ikegami commented Aug 29, 2020

When a JS function returns nothing, it's return value is undefined.

So I believe the spec intends the following:

  • 2.2.7.1. Returning nothing is the same as returning undefined (JS) or nil (Lua).
  • 2.2.7.1. Returned values beyond the first are ignored.
  • 2.3.3.3.1. Being called with no arguments is the same as being called with undefined (JS) or nil (Lua).
  • 2.3.3.3.1. Arguments beyond the first are ignored.

To that end, I too believe the value == nil check in transition should be removed.

@4O4
Copy link
Author

4O4 commented Oct 18, 2020

Sadly, this project seems to be abandoned ☹️

recih added a commit to recih/promise.lua that referenced this issue Dec 23, 2020
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

4 participants