lua-users home
lua-l archive

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


Michal Kolodziejczyk <miko@wp.pl> dixit:

> On 18.12.2009 18:19, Javier Guerra wrote:
> > On Fri, Dec 18, 2009 at 12:07 PM, spir <denis.spir@free.fr> wrote:
> >> PS: There are certainly several techniques (eg thought at requiring a table for opt args). I'd like to review various possibilities.
> > 
> > roughly in order of personal preference:
> > 
> > - try to design the API so that it doesn't make sense to have more
> > than 1 optional parameter
> 
> This is interesting... That would be kind of dataflow programming:
> 
> http://en.wikipedia.org/wiki/Dataflow_programming
> 
> Here is an example (but this assumes some form of object oriented
> programming):
> 
> object={
>   setX=function(self, value)
>     self.X=value
>     return self
>   end,
>   setY=function(self, value)
>     self.Y=value
>     return self
>   end,
>   setZ=function(self, value)
>     self.Z=value
>     return self
>   end
> }
> setmetatable(object, {__tostring=function(self)
>   return(string.format("X=%s Y=%s Z=%s", tostring(self.X),
> tostring(self.Y), tostring(self.Z)))
>   end
> })
> 
> object:setX(1):setZ(3) -- instead of object:setXYZ(1,nil,3)
> print(object)
> 
> Regards,
> 	miko
> 

This is a common pattern of concatenative/postfix languages -- for the reason the last result is always on top of the stack. One could write eg:
   object 1 setX 3 setZ
and even chain with print or whatever:
   object 1 setX 3 setZ print

Some (OO) languages like Io also return the object itself by default, thus allowing such as pipe-like programming pattern. I find this default really attractive. For once, code density does not conflict with clarity.

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/