Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ A function with no arguments can be called with them?
A function with no arguments can be called with them?
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Cadari
(25 posts) Bio
|
Date
| Wed 29 Aug 2007 06:03 PM (UTC) |
Message
| Hello. A very basic question.
function balance_equilibrium_notify()
...
end
balance_equilibrium_notify(gained_balance_eq)
A function with no parameters is called with a one? Is that a trick of Lua of some kind? :) gained_balance_eq is boolean if it's of any importance. | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #1 on Wed 29 Aug 2007 06:28 PM (UTC) |
Message
| I'm not quite sure what exactly the question is here... Yes, you can call a function that does not take any parameters with a parameter in it... It just doesn't do particularly much of anything.
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
> function test()
>> print( "whee!" )
>> end
> test( true )
whee!
> test( "foo" )
whee!
>
|
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Cadari
(25 posts) Bio
|
Date
| Reply #2 on Wed 29 Aug 2007 06:56 PM (UTC) |
Message
| I don't know a single thing about Lua, so was wandering, if it is some kind of work-around or something alike. Like not calling the mentioned function if the parameter is nil.
Thank you! | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) Bio
|
Date
| Reply #3 on Wed 29 Aug 2007 07:11 PM (UTC) |
Message
|
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio
> function test( foo, bar )
>> print( foo, bar )
>> end
> test( 1, 2 )
1 2
> test( 1 )
1 nil
> test()
nil nil
>
Is this what you mean? You can check to see if a parameter has been passed before running the function.
Personally, with the example you gave, I would just test before calling the function.
if gained_balance_eq ~= nil then
balance_equilibrium_notify(gained_balance_eq)
end
|
It is much easier to fight for one's ideals than to live up to them. | Top |
|
Posted by
| Cadari
(25 posts) Bio
|
Date
| Reply #4 on Wed 29 Aug 2007 09:15 PM (UTC) Amended on Wed 29 Aug 2007 09:17 PM (UTC) by Cadari
|
Message
| No, sorry, I'm not that good with explaining my thoughts in English.
The code I posted is not mine. I just stumbled upon it and thought there are two possibilities:
1. It's just a minor mistake, nothing to worry about if Lua allows it.
2. It's an extremely complex Lua-only trick and this code works in some completely unexpected way :)
Now, as you told me it's just #1, I have no questions. Thanks, again. | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #5 on Wed 29 Aug 2007 09:56 PM (UTC) |
Message
|
Quote:
A function with no parameters is called with a one? Is that a trick of Lua of some kind?
The Lua manual clearly illustrates that functions can be called with more or less arguments than they "expect". If you supply too many arguments, they are ignored. If you supply "too few" then the extra ones are set to nil.
I cannot see a huge amount of point in writing code like your example, as calling that function with an argument, which it will not handle, seems redundant and is probably a simple coding error. However it is not a Lua syntax error.
Functions can also return multiple results, a "trick" which is used quite often. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
18,481 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top