[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How do you cope with 2 or more optional parameters?
- From: Michal Kolodziejczyk <miko@...>
- Date: Fri, 18 Dec 2009 19:05:58 +0100
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