lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Alejandro Reimondo once stated:

> Copied to the list becasue I want to share the response  to help me find a
> way to resolve the problem
> 
> Hi Egor,
> 
> >What does "send messages to functions" mean?
> 
> In object paradigm (Object Technology) the only way to  interact with an
> object is sending messages. We/anObject impact other object sending
> messages. To send a message we need a receiver that is the object that
> will react to the message sent.
> 
> In object ORIENTED languages, e.g. languages that defines  entities that
> are not objects, the problem impose limitations  for changes during the
> lifetime of the system (the system is not open in all directions).
> 
> In Lua we have tables (that can be used to implement objects) and values.
> The values (string, function, boolean, nil) impose rigid semantics and do
> not let the system evolve in that corners. The case of Strings in Lua, can
> be patched changing the metatable  of one string (all strings share the
> same metatable). As I know, Functions do not share a standard metatable,
>  and we can´t apply the same mechanism to extend semantics  of functions.
> 
> Changing the metatabe of each function is not a solution,  because
> functions are allocated anytime and anywhere.
> 
> With the information & knowledge I have today on Lua,  I do not see a way
> to extend all functions of a system  modifying one object/metatable.
> 
> hope the explanation helps to understand what I need to resolve. w/best
> regards,

In a quick experiment:

[spc]lucy:~>lua-53 
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> x = function(x) return 3 * x + 5 end
> y = function(x) return 4 * x - 3 end
> print(debug.getmetatable(x))
nil
> mt = { }
> debug.setmetatable(x,mt)
function: 0x81f9598
> print(debug.getmetatable(x))
table: 0x81e7de8
> print(debug.getmetatable(y))
table: 0x81e7de8
> print(debug.getmetatable(print))
table: 0x81e7de8
> print(debug.getmetatable(io.open))
table: 0x81e7de8
> 

It seems that setting the metatable on one function set the metatable on all
functions, so it appears that functions share the same metatable.

  -spc