lua-users home
lua-l archive

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

Alexander Sorockin wrote:
Hi All,

I guess the most likely answer to this question is 'No', but just in case. Is there a way to implement in C a function call as an lvalue? Basically, I want my Lua program to be able to do something like this:

    m = mymessage.create()
    m.byte(1).bits(2, 3) = 2

No :) Well, not without hacking the Lua sources, since that doesn't make it past the parser, so it's not supported syntax.

I know I can implement something like this:

    m.byte(1).bits(2, 3).set(2)

or this:

    m.byte[1].bits[2][3] = 2

But the former looks more elegant to me :)

I'd do

	m.byte(1).bits(2, 3, ...)

where the last parameters are replacements, but if you really want to index using ranges you could try:

	m.byte(1).bits[{2, 3}] = 2

with the cost of creating and throwing away a table (and of course having "bits" be a table with a __call metamethod, instead of a function).