How to comprehend "in Lua, all functions are anonymous. Like any other value, they do not have names. " at a deeper level?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to comprehend "in Lua, all functions are anonymous. Like any other value, they do not have names. " at a deeper level?

孙世龙 sunshilong
Hi, list

How to comprehend "in Lua, all functions are anonymous. Like any other
value, they do not have names. " at a deeper level?

I know that the named function syntax function add(a,b) return a+b end
is actually a syntactic sugar for add = function(a,b) return a+b end.
And I  understand that you can specify the sorting order for
table.sort() by anonymous function.

Why does Lua prefer to anonymous functions, unlike the functions in C?
What're the advantages comparing with providing non-anonymous function syntax?

Thank you for your attention to my question.
Looking forward to hearing from you.

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: How to comprehend "in Lua, all functions are anonymous. Like any other value, they do not have names. " at a deeper level?

Philippe Verdy-2
function in C can also be anonymous, or have several names. The association is in the exported symbols for the linker, inside a dictionary that maps symbols to anonymous function entry points (plus some metadata allowing to load its code either at link time or at runtime; the names used by the linker are not the same as those exposed in the source code, even without using any macrosubstitution, because the compiler may "decorate" it, for example to add a signature for its datatype or its calling convention).

But at runtime (after the code is loaded) these functions are also anonymous, but they may eventually be exposed in a reflection API or a debugger (if debugging info was generated and not stripped from the binary), but if you've traced C programs, there are frequent cases where no symbol is even generated and all you get is a base symbol plus some offset.... Finally the initial symbols used at link time is not necessarily the same as the name that may be exported (or it may not be exported at all, especially "static" functions, accessible only inside the current module, or by an anonymous function pointer passed from one module to another, for example for callbacks).


Le mar. 13 oct. 2020 à 03:39, 孙世龙 sunshilong <[hidden email]> a écrit :
Hi, list

How to comprehend "in Lua, all functions are anonymous. Like any other
value, they do not have names. " at a deeper level?

I know that the named function syntax function add(a,b) return a+b end
is actually a syntactic sugar for add = function(a,b) return a+b end.
And I  understand that you can specify the sorting order for
table.sort() by anonymous function.

Why does Lua prefer to anonymous functions, unlike the functions in C?
What're the advantages comparing with providing non-anonymous function syntax?

Thank you for your attention to my question.
Looking forward to hearing from you.

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: How to comprehend "in Lua, all functions are anonymous. Like any other value, they do not have names. " at a deeper level?

Matěj Cepl
In reply to this post by 孙世龙 sunshilong
孙世龙 sunshilong píše v Út 13. 10. 2020 v 09:38 +0800:
> How to comprehend "in Lua, all functions are anonymous. Like any other
> value, they do not have names. " at a deeper level?

https://en.wikipedia.org/wiki/First-class_function

The same goes for all functional languages, but it is also the
saving grace for JavaScript, and it is present (although not
that prominent) in Python. For most classical (C/C++, Java,
Pascal, etc.) functions are completely different animals from
values which are manipulated inside of them. For these modern
languages, functions are just same objects as integers.

Matěj

--
https://matej.ceplovi.cz/blog/, Jabber: [hidden email]
GPG Finger: 3C76 A027 CA45 AD70 98B5  BC1D 7920 5802 880B C9D8
 
I would like to die sleeping, like my father — rather than
screaming and helpless, like his passengers.


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to comprehend "in Lua, all functions are anonymous. Like any other value, they do not have names. " at a deeper level?

Sam Putman
In reply to this post by 孙世龙 sunshilong


On Mon, Oct 12, 2020 at 3:39 PM 孙世龙 sunshilong <[hidden email]> wrote:
Hi, list

How to comprehend "in Lua, all functions are anonymous. Like any other
value, they do not have names. " at a deeper level?


in Lua, functions are values.

so you can do this:

function aFn(a, b)
   return a + b
end

and do this

b = aFn

then

b(2,3) -- returns 5

and also

c = {b}
c[1](3,4) -- returns 7

or this

d = function()
       return aFn
    end ()
d(5,6) -- returns 11

Functions are just values, and a module, any Lua program, is just a function as well.

Hope that helps,
-Sam