lua-users home
lua-l archive

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


If one is going to play those sort of games, I would think -- not enough coffee yet to write -- that one could define functions to let one do the following:

	a = Expand {
		one = {
			a = 1,
			b = Ref 'two'
		},
		two = {
			a = 1,
			b = Ref 'one',
			c = Ref 'one.b'
		}
	}

The evaluation model is that Ref captures the path string as a special reference object and Expand recurses through the table looking for refs and evaluating them. In the above, I allowed for more complex paths which makes Expand's job a little bit harder particularly with regard to resolving mutually recursive references.

The nice thing about an approach like this is that Expand could deal with other constructions as well. For example, one could have an unpack entry which would expand appropriately and shove everything down or an optional entry that would get removed if not used:

	commands = Expand {
		Unpack( get_standard_commands() ),
		If( its_dark(), "turn_on_the_lights" ),
		If( its_tuesday(), "pick_another_card" )
	}

I will note that this is where some other syntactic extension games become interesting because it would be nice to make functions like Unpack, If, and Ref only available as globals within an Expand usage while allowing a linter or runtime check to complain about their use elsewhere. 

Mark

On Oct 30, 2011, at 8:49 AM, Philipp Janda wrote:

> Oops, missing return.
> 
> On 30.10.2011 16:07, Philipp Janda wrote:
>> $ cat > ref.lua
>> local temp = {}
>> local function v( s )
>>  return function( t )
>>    temp[ s ] = t
>      return t
>>  end
>> end
> 
> Mmmh, if you only need references to subtables, you could do even better ...
> 
> $ cat > ref2.lua
> local temp = {}
> local function v( s )
>  return function( t )
>    if temp[ s ] then
>      for k,v in pairs( t ) do
>        temp[ s ][ k ] = v
>      end
>    else
>      temp[ s ] = t
>    end
>    return temp[ s ]
>  end
> end
> local function r( s )
>  if not temp[ s ] then
>    temp[ s ] = {}
>  end
>  return temp[ s ]
> end
> 
> a = {
>  one = v"one"{
>    a = 1,
>    b = r"two"
>  },
>  two = v"two"{
>    a = 1,
>    b = r"one"
>  }
> }
> 
> print( a.two.b.b.b.b.b.b.a )
> ^D
> 
> Philipp (<- having a look at his old serializer/pretty-printer)
> 
> 
> 
>