|
| 31 Dec 2013 01:34 PM |
function GetNumber(a) if a == 1 then return (function() print(a); end) elseif a == 2 then return (function() print(a); end) end end
d = GetNumber(1) d()
Prints 1. This has alot of potential in my mind for like maybe a substitute for properties? just return different functions depending on the variable a. |
|
|
| Report Abuse |
|
|
lombardo2
|
  |
| Joined: 30 Nov 2008 |
| Total Posts: 1604 |
|
|
| 31 Dec 2013 01:51 PM |
| Fucntions are not variables. In Lua, functions are first class citizens, meaning you can pass them as arguments, return them from other functions and assign them to variables, but functions are not variables, they are different concepts. |
|
|
| Report Abuse |
|
|
|
| 31 Dec 2013 02:04 PM |
You can use them as variables, kinda
k = function
I was told that they were and it doesnt matter they work the same way. |
|
|
| Report Abuse |
|
|
|
| 31 Dec 2013 02:04 PM |
Functions are variables. They're stored as a different TYPE of variable, and they're the only ones you can call.
You can initialize functions just like other variables
local funct = function() print("hi") end
funct()
and they can be used just like any other variable, and also be called as you would want to do for the function |
|
|
| Report Abuse |
|
|
|
| 31 Dec 2013 02:10 PM |
| Yes, I just thought this had so much potential for like virtual properties. |
|
|
| Report Abuse |
|
|
morash
|
  |
| Joined: 22 May 2010 |
| Total Posts: 5834 |
|
|
| 31 Dec 2013 02:17 PM |
| You can actually concatenate functions onto a string, but it gives some weird code, which might be considered by some to show that Lua functions are variables. Putting cat = function() print("Hi") end print(cat) into the command bar demonstrates that functions can be set a variables and shown as variables. |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
|
| 31 Dec 2013 02:32 PM |
"Functions are variables. They're stored as a different TYPE of variable, and they're the only ones you can call."
No. No. And more no.
@Everyone Functions aren't variables. Just the same as 2 isn't a variable. However, variables can be functions, or hold a function, if you will. The code [ function a() end ] is translated to (or treated as) [ a = function() end ], so they aren't special monsters of doom. They are simple and wonderful little creatures. A special thing about functions (or function calls, I should say) is that they can be both a statement and an expression.
@OP Yes, returning a function can add certain features, although I don't see a situations where you want to return different things. You generally want your stuff to be consistent, and you want to know what you're working with. Instead of having one function to check your values, and return different functions depending on that, you should rather just call those different functions from the start.
It seems to me like you think of the code a bit too little programmer-y. You think of certain mixes of certain things doing certain things, when what you should be thinking of is what certain individual things do. Then you can mix them together and do whatever you want, as well as have a completely clear understanding of things. This actually applies to a lot of people, probably like half of Scripters or something. |
|
|
| Report Abuse |
|
|
|
| 31 Dec 2013 06:11 PM |
@os well I know a lot about Lau so I try to expand my knowledge by doing what you said, using tools in ways almost never thought of. I can think of some practicable uses of this such as a global funcyion that controls the whole game.
|
|
|
| Report Abuse |
|
|