lua-users home
lua-l archive

Conditional Function Definition

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


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

Thanks in advanced

Marcello Chiuminatto E.


AltStyle によって変換されたページ (->オリジナル) /