# Conditional Function Definition Classic List Threaded 6 messages Open this post in threaded view
|

## Conditional Function Definition

 Hi everybody Maybe someone can help with this. I have “polymorphism” situation where I want to define a function that depending on a condition will have different signature and behaviors I would like something like this, but unfortunately does not work. Is there any other way to implement this in lua? MyClass = {} If somecondition == 1 thenMyClass.f = function (self, x)               return x*2 endend if somecondition == 2 thenMyClass.f = function (self, x,y)               return x*yendend if somecondition==3 then – this case escapes from polymorphism since have the same signature that f in somecondition == 2, but I need this anyway.MyClass.f = function (self, x,y)               return x/1endend  Thanks in advanced Marcello Chiuminatto E.
Open this post in threaded view
|

## Re: Conditional Function Definition

 It was thus said that the Great Marcello Chiuminatto once stated: > Hi everybody > > Maybe someone can help with this. > > I have "polymorphism" situation where I want to define a function that > depending on a condition will have different signature and behaviors > > I would like something like this, but unfortunately does not work. Is > there any other way to implement this in lua? > > MyClass = { > } > > If somecondition == 1 then >   MyClass.f = function (self, x) >                return x*2 >   end > end > > if somecondition == 2 then >   MyClass.f = function (self, x,y) >                return x*y >   end > end > > if somecondition == 3 then >   -- this case escapes from polymorphism since have the same signature >   -- that f in somecondition == 2, but I need this anyway. > >   MyClass.f = function (self, x,y) >                return x/1 >   end > end   What do you mean by "does not work"?  What error are you seeing?  Because from what I can see, this will work (you can always pass more parameters than a Lua function expects---said function will just ignore the extra parameters).     -spc
Open this post in threaded view
|

## Re: Conditional Function Definition

 In reply to this post by Marcello Chiuminatto 2017-03-19 1:50 GMT+02:00 Marcello Chiuminatto <[hidden email]>: > I have “polymorphism” situation where I want to define a function that > depending on a condition will have different signature and behaviors Lua has many mechanisms for polymorphism, but none of them work exactly as in C++, since the type of a Lua value at runtime can be anything. Your code does not work because it tests the function condition at compile time (i.e. only one of the forms of MyClass.f get compiled) whereas true polymorphism requires that the results depend on what arguments you pass. In C terms, you are in effect using #ifdef. Here for example is a function polymorphic on the number of arguments. morphs = {math.exp,math.log,math.max} function poly1(...)    return morphs[math.min(select('#',...),3)](...) end
Open this post in threaded view
|

## RE: Conditional Function Definition

 My approach was wrong, because I was trying to assign the function at loading time (upon require execution) when what I really needed was that the function assignment was in a object by object basis.  This post opened my eyes and finally I found a solution. Thanks, this will save me a lot of lines and ifs when using the module. MyClass.lua MyClass = {                              myType = "",                              myCatalog ={type1={}, type2={},type3={},},                              myVar =0                                             function MyClass:new (type)               o =  {}   -- create object if user does not provide one               setmetatable(o, self)               self.__index = self               o:createCatalog()               o.f = self.myCatalog[type].f               return oend function MyClass:createCatalog()                self.myCatalog["type1"]["f"] = function(self, x)                                                            return x*2;               end                self.myCatalog["type2"]["f"] = function(self, x, y)               return x*y;               end                self.myCatalog["type3"]["f"] = function(self, x, y)               return x/y;               endend MyClassUser.lua  require("MyClass")local obj1 = MyClass:new( "type1");local obj2 = MyClass:new("type2");local obj3 = MyClass:new("type3"); print (obj1:f(2))print (obj2:f(2,3))print (obj3:f(2,3)) >lua -e "io.stdout:setvbuf 'no'" "MyClassUser.lua" 460.66666666666667 -----Original Message-----From: [hidden email] [mailto:[hidden email]] On Behalf Of Dirk LaurieSent: Sunday, March 19, 2017 3:20 AMTo: Lua mailing list <[hidden email]>Subject: Re: Conditional Function Definition 2017-03-19 1:50 GMT+02:00 Marcello Chiuminatto <[hidden email]>: > I have “polymorphism” situation where I want to define a function that > depending on a condition will have different signature and behaviors Lua has many mechanisms for polymorphism, but none of them work exactly as in C++, since the type of a Lua value at runtime can be anything. Your code does not work because it tests the function condition at compile time (i.e. only one of the forms of MyClass.f get compiled) whereas true polymorphism requires that the results depend on what arguments you pass. In C terms, you are in effect using #ifdef. Here for example is a function polymorphic on the number of arguments. morphs = {math.exp,math.log,math.max} function poly1(...)   return morphs[math.min(select('#',...),3)](...)end