Lua function syntax sugar for a digit started function name?

classic Classic list List threaded Threaded
5 messages Options
y s
Reply | Threaded
Open this post in threaded view
|

Lua function syntax sugar for a digit started function name?

y s

Lua 5.1.4

For example:

bar = {} 
bar.name = 'test' 
bar['123.com'] = function(self) print(self.name) end 

I can't call the method like below:

bar:['123.com']() 
stdin:1: '<name>' expected near '[' 

Althought below works:

bar['123.com'](bar) 

But I this is somehow ugly. Is there a syntax sugar for this situation?

Or if it really cannot do it, will Lua team add this syntax in future?

Or Lua team did this intentionally?

Reply | Threaded
Open this post in threaded view
|

Re: Lua function syntax sugar for a digit started function name?

Dirk Laurie-2
> Lua 5.1.4
>
> For example:
>>
>> bar = {}
>> bar.name = 'test'
>> bar['123.com'] = function(self) print(self.name) end
>
> I can't call the method like below:
>>
>> bar:['123.com']()
>> stdin:1: '<name>' expected near '['
>

This has nothing to do with function names.  The tbl.xxx and tbl:xxx
syntax sugar
is available only when xxx is an alphabetic key.

Reply | Threaded
Open this post in threaded view
|

Re: Lua function syntax sugar for a digit started function name?

Tony Finch
In reply to this post by y s
y s <[hidden email]> wrote:
>
>  I can't call the method like below:
>
> > bar:['123.com']()

Try the patch below.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Hebrides: East or northeast 3 or 4, becoming cyclonic 4 or 5. Moderate or
rough. Rain or showers. Moderate or good.


diff --git a/src/lcode.c b/src/lcode.c
index 6194749..0ae8722 100644
--- a/src/lcode.c
+++ b/src/lcode.c
@@ -500,16 +500,16 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
 }


-void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
-  int func;
+void luaK_self1 (FuncState *fs, expdesc *e) {
   luaK_exp2anyreg(fs, e);
   freeexp(fs, e);
-  func = fs->freereg;
   luaK_reserveregs(fs, 2);
-  luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
+}
+
+
+void luaK_self2 (FuncState *fs, expdesc *e, expdesc *key) {
+  luaK_codeABC(fs, OP_SELF, e->u.s.info, e->u.s.info, luaK_exp2RK(fs, key));
   freeexp(fs, key);
-  e->u.s.info = func;
-  e->k = VNONRELOC;
 }


diff --git a/src/lcode.h b/src/lcode.h
index b941c60..80b3c4e 100644
--- a/src/lcode.h
+++ b/src/lcode.h
@@ -55,7 +55,8 @@ LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
 LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
+LUAI_FUNC void luaK_self1 (FuncState *fs, expdesc *e);
+LUAI_FUNC void luaK_self2 (FuncState *fs, expdesc *e, expdesc *key);
 LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
 LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
diff --git a/src/lparser.c b/src/lparser.c
index 7ff0925..956b46e 100644
--- a/src/lparser.c
+++ b/src/lparser.c
@@ -780,8 +780,12 @@ static void primaryexp (LexState *ls, expdesc *v) {
       case ':': {  /* `:' NAME funcargs */
         expdesc key;
         luaX_next(ls);
-        checkname(ls, &key);
-        luaK_self(fs, v, &key);
+        luaK_self1(fs, v);
+        if (ls->t.token == '[')
+          yindex(ls, &key);
+        else
+          checkname(ls, &key);
+        luaK_self2(fs, v, &key);
         funcargs(ls, v);
         break;
       }

Reply | Threaded
Open this post in threaded view
|

Re: Lua function syntax sugar for a digit started function name?

Mark Hamburg
In reply to this post by y s
I wrote a patch on 5.1.something a while ago to support :[ expression ] as a way to invoke OP_SELF. As I recall, I had to patch both the parser (obviously) and OP_SELF though both changes were minor.

It definitely helps syntactically for cases where you want to do things like register callbacks to invoke — e.g., a weak table of object to selector mappings to be used for notification. It also allows one to have private methods by using hidden tables as keys.

I haven't kept my patch around because I have been trying to stick closer to stock Lua more of the time, but if I could make one syntactic change in Lua it probably would be this. (That probably gets followed with wanting obj:method and obj:[ expression ] when used outside of a call context to be shorthand for creating an appropriate closure object, but that's a much more complicated patch and there are interesting questions about whether it should do the method lookup immediately or at call time.)

Mark

On Apr 19, 2012, at 7:10 AM, y s <[hidden email]> wrote:

Lua 5.1.4

For example:

bar = {} 
bar.name = 'test' 
bar['123.com'] = function(self) print(self.name) end 

I can't call the method like below:

bar:['123.com']() 
stdin:1: '<name>' expected near '[' 

Althought below works:

bar['123.com'](bar) 

But I this is somehow ugly. Is there a syntax sugar for this situation?

Or if it really cannot do it, will Lua team add this syntax in future?

Or Lua team did this intentionally?

Reply | Threaded
Open this post in threaded view
|

Re: Lua function syntax sugar for a digit started function name?

William C. Bubel
In reply to this post by y s
On 04/19/2012 10:10 AM, y s wrote:

> Lua 5.1.4
>
> For example:
>
>     |bar = {}
>     bar.name = 'test'
>     bar['123.com <http://123.com>'] = function(self) print(self.name) end
>     |
>
> I can't call the method like below:
>
>     |bar:['123.com <http://123.com>']()
>     stdin:1: '<name>' expected near '['
>     |

Maybe this solution might help.

  bar = {}
  bar.name = 'test'
  bar['123.com <http://123.com>'] = function(self) print(self.name) end

  -- Make bar behave like a function
  setmetatable(bar,{__call=function(t, k, ...) return t[k](t,...) end})

  bar('123.com <http://123.com>')