Assign idiom

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Assign idiom

Leigh McRae-2
Hi all,
 
  I find myself repeating the same type of code over and over.  Is there an idiom for this type of thing?
 
self.spBackGround = CreateSprite()
 
local    sp = self.spBackGround
sp:setSize( 10, 10 )
 
  I am looking for something along the lines of:
 
local    sp = self.spBackGround = CreateSprite()
sp:setSize( 10, 10 )
 
Leigh McRae
Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Luiz Henrique de Figueiredo
> local    sp = self.spBackGround = CreateSprite()
> sp:setSize( 10, 10 )

If you don't need sp after that, try
	CreateSprite():setSize( 10, 10 )


Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Leigh McRae-2
Yeah, I need sp after that. The pattern is that I create a sprite to be stored in 'self' and then configure sp with calls. Thanks anyways.

----- Original Message ----- From: "Luiz Henrique de Figueiredo" <[hidden email]>
To: "Lua list" <[hidden email]>
Sent: Thursday, March 08, 2007 12:09 PM
Subject: Re: Assign idiom


local    sp = self.spBackGround = CreateSprite()
sp:setSize( 10, 10 )

If you don't need sp after that, try
CreateSprite():setSize( 10, 10 )





Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Rob Heyes
Leigh McRae wrote:

Some people may frown on this, but your sprite's parameter setting methods could each return self
then you could use code like

  self.spBackGround = CreateSprite():setSize(10, 10):setName('MySprite') etc.

Another possibility is to pass a table to CreateSprite with the values e.g.

  self.spBackGround = CreateSprite({Width = 10, Height = 10, Name = 'MySprite'})

Yeah, I need sp after that. The pattern is that I create a sprite to be stored in 'self' and then configure sp with calls. Thanks anyways.

----- Original Message ----- From: "Luiz Henrique de Figueiredo" <[hidden email]>
To: "Lua list" <[hidden email]>
Sent: Thursday, March 08, 2007 12:09 PM
Subject: Re: Assign idiom


local    sp = self.spBackGround = CreateSprite()
sp:setSize( 10, 10 )

If you don't need sp after that, try
CreateSprite():setSize( 10, 10 )









Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Leigh McRae-2
This actually gets me thinking. I find I tend to think as a C coder in all the languages I use. So maybe I just need to put my lua hat on and figure out a way. The chaining is interesting but I tend to use sp to much, hence why I make it a local. Good food for thought though. I would have thought there would be something for this since lua uses tables for everything. I suspect lots of people do this kind of thing.

----- Original Message ----- From: "Rob Heyes" <[hidden email]>
To: "Lua list" <[hidden email]>
Sent: Thursday, March 08, 2007 12:33 PM
Subject: Re: Assign idiom


Leigh McRae wrote:

Some people may frown on this, but your sprite's parameter setting methods could each return self
then you could use code like

self.spBackGround = CreateSprite():setSize(10, 10):setName('MySprite') etc.

Another possibility is to pass a table to CreateSprite with the values e.g.

self.spBackGround = CreateSprite({Width = 10, Height = 10, Name = 'MySprite'})

Yeah, I need sp after that. The pattern is that I create a sprite to be stored in 'self' and then configure sp with calls. Thanks anyways.

----- Original Message ----- From: "Luiz Henrique de Figueiredo" <[hidden email]>
To: "Lua list" <[hidden email]>
Sent: Thursday, March 08, 2007 12:09 PM
Subject: Re: Assign idiom


local    sp = self.spBackGround = CreateSprite()
sp:setSize( 10, 10 )

If you don't need sp after that, try
CreateSprite():setSize( 10, 10 )












Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Michael Broughton
In reply to this post by Leigh McRae-2
Use multiple returns...

function CreateSprite()
   ...
   return sp, sp
end

local sp
self.spBackGround, sp = CreateSprite()


Or...

function f(p)
   return p, p
end

local sp
self.spBackGround, sp = f(CreateSprite())



Or perhaps something a little different... Have each of your sprite's methods return its first argument...

self.spBackGround = CreateSprite()
local sp = self.spBackGround:setSize( 10, 10 ):doSomething()
sp:doSomethingElse()


Mike



Leigh McRae wrote:
Hi all,
I find myself repeating the same type of code over and over. Is there an idiom for this type of thing? self.spBackGround = CreateSprite() local sp = self.spBackGround
sp:setSize( 10, 10 )
I am looking for something along the lines of: local sp = self.spBackGround = CreateSprite()
sp:setSize( 10, 10 )
Leigh McRae

Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Alexander Gladysh
In reply to this post by Leigh McRae-2
>> local    sp = self.spBackGround = CreateSprite()
>> sp:setSize( 10, 10 )

What about something like this? (All code right from my head, without
syntax check.)

 -- Meaning a member function defined somewhere
 self.InitBGSprite = function(self)
   self.spBackGround = CreateSprite()
   return self.spBackGround
 end

 local sp = self.InitBGSprite()

Or even this (looks like overkill though, I like previous one more):

 -- Meaning a member function defined somewhere
 self.InitVar = function(self, name, ctorfn, ...)
   local v = ctorfn(...)
   self[name] = v
   return v
 end

 local sp = self.InitVar("spBackGround", CreateSprite)

 -- If your CreateSprite() accepts some extra arguments, say, size,
 -- you can do this instead:
 local sp2 = self.InitVar("spBackGround", CreateSprite, 800, 600)

If you're going to use this idiom very often, you can use combined approach:

 -- A global function constructor
 MakeInitializer = function(name, ctorfn)
   return function(self, ...)
     local v = ctorfn(...)
     self[name] = v
     return v
   end
 end

 -- Meaning a member function defined somewhere
 self.InitBGSprite = MakeInitializer("spBackGround", CreateSprite)

 local sp = self.InitBGSprite()
 -- or --
 local sp1 = self.InitBGSprite(800, 600)

HTH,
Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Doug Rogers-4
In reply to this post by Leigh McRae-2
Leigh McRae wrote:

>   I am looking for something along the lines of:
> local    sp = self.spBackGround = CreateSprite()
> sp:setSize( 10, 10 )

If setSize() returns the sprite, then:

  self.spBackGround = CreateSprite():setSize(10, 10)

It's hard to see a way to avoid a multiple assignment to capture to a
local variable. Perhaps:

  function dup(x) return x, x end

  local sp
  sp, self.spBackGround = dup(CreateSprite():setSize(10, 10))

Looks like that one was already suggested! You might also try:

  function self:set(k, v)
     self[k] = v
     return v
  end

  local sp = self:set('spBackGround', CreateSprite()):setSize(10,10)

I think your original code is easier to read.

Doug

-- 
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220

Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Leigh McRae-2
Actually I think your last one is really good. I was also thinking of the multi-return idea but some of the code comes from C++ bindings. So W_TextRegion() is a wrapper for my C++ class. I didn't really want to change all my C++ code to suit lua.

 self.spName = W_TextRegion()

local sp = self.spName
sp:autoSize()
sp:setJustify( W_TextObject_Left )
sp:setFont( UI_Fonts.Comic_18 )
sp:anchor( W_Region_TopLeft, W_Region_TopLeft, self:getRegion( W_Region_Center ) )
self:addRegion( "FOREGROUND", sp )

 So I could the following:

function SetIdiom(tb, v)
   tb = v
   return v
end

local sp = SetIdiom( self.spBackGround, W_TextRegion() )

 Not bad, little hard on the eyes :)

----- Original Message ----- From: "Doug Rogers" <[hidden email]>
To: "Lua list" <[hidden email]>
Sent: Thursday, March 08, 2007 12:58 PM
Subject: Re: Assign idiom


Leigh McRae wrote:

  I am looking for something along the lines of:
local    sp = self.spBackGround = CreateSprite()
sp:setSize( 10, 10 )

If setSize() returns the sprite, then:

 self.spBackGround = CreateSprite():setSize(10, 10)

It's hard to see a way to avoid a multiple assignment to capture to a
local variable. Perhaps:

 function dup(x) return x, x end

 local sp
 sp, self.spBackGround = dup(CreateSprite():setSize(10, 10))

Looks like that one was already suggested! You might also try:

 function self:set(k, v)
    self[k] = v
    return v
 end

 local sp = self:set('spBackGround', CreateSprite()):setSize(10,10)

I think your original code is easier to read.

Doug

--
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220




Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Mark Edgar-2
On 3/8/07, Leigh McRae <[hidden email]> wrote:
function SetIdiom(tb, v)
    tb = v
    return v
end

local sp = SetIdiom( self.spBackGround, W_TextRegion() )

This will not work.  tb is local to the function body; modifying it
does not modify the actual argument.

The following will work:

function set(t,k,v)
 t[k] = v
 return v
end

local sp = set( self, "spBackGround", W_TextRegion() )

     -Mark

Reply | Threaded
Open this post in threaded view
|

RE: Assign idiom

Bilyk, Alex
In reply to this post by Rob Heyes
I was also thinking table here. It would probably be more efficient as
well, compared to a bunch of function calls.

-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Rob Heyes
Sent: Thursday, March 08, 2007 9:34 AM
To: Lua list
Subject: Re: Assign idiom

Leigh McRae wrote:

Some people may frown on this, but your sprite's parameter setting
methods could each return self
then you could use code like

   self.spBackGround = CreateSprite():setSize(10,
10):setName('MySprite') etc.

Another possibility is to pass a table to CreateSprite with the values
e.g.

   self.spBackGround = CreateSprite({Width = 10, Height = 10, Name =
'MySprite'})

> Yeah, I need sp after that.  The pattern is that I create a sprite to
be 
> stored in 'self' and then configure sp with calls. Thanks anyways.
> 
> ----- Original Message ----- From: "Luiz Henrique de Figueiredo" 
> <[hidden email]>
> To: "Lua list" <[hidden email]>
> Sent: Thursday, March 08, 2007 12:09 PM
> Subject: Re: Assign idiom
> 
> 
>>> local    sp = self.spBackGround = CreateSprite()
>>> sp:setSize( 10, 10 )
>>
>> If you don't need sp after that, try
>> CreateSprite():setSize( 10, 10 )
>>
>>
> 
> 
> 
> 




Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Etan Reisner
In reply to this post by Leigh McRae-2
On Thu, Mar 08, 2007 at 01:11:24PM -0500, Leigh McRae wrote:
> Actually I think your last one is really good.  I was also thinking of the
> multi-return idea but some of the code comes from C++ bindings.  So
> W_TextRegion() is a wrapper for my C++ class.  I didn't really want to
> change all my C++ code to suit lua.

Your wrapper function can do anything it wants without you needing to
change any of your 'real' C++ code. That is you can make the lua exported
W_TextRegion() function take a table of values keyed by mnemonic names for
their settings (so 'auto_size', 'justify', 'font', 'anchor', etc.) and
then have the C wrapper function call each function in succession as it
walks the argument table. Thus requiring no changes to the way your C++
functions work and still giving you a nice lua scripting interface.

Making the lua wrappers 'dumb' is certainly the easiest way to do things,
and it is good for a first try, but making it 'smart' makes for a much
more friendly scripting interface.

    -Etan

Reply | Threaded
Open this post in threaded view
|

Re: Assign idiom

Doug Rogers-4
In reply to this post by Mark Edgar-2
Mark Edgar wrote:
> On 3/8/07, Leigh McRae <[hidden email]> wrote:
>> function SetIdiom(tb, v)
>>     tb = v
>>     return v
>> end
>> local sp = SetIdiom( self.spBackGround, W_TextRegion() )
> This will not work.  tb is local to the function body; modifying it
> does not modify the actual argument.
> The following will work:
> function set(t,k,v)
>  t[k] = v
>  return v
> end
> local sp = set( self, "spBackGround", W_TextRegion() )

Correct. I was careful to insert the set() function into self so that it
would work. I actually tried it before I posted it, so I know it worked
as written in my message. But you definitely need three arguments (my
first one was the hidden self argument of the ':' syntactic sugar), and
you definitely need to put the name of the field in quotes.

You (Leigh) might want to consider Etan's advice, too.

Doug

-- 
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220