Skip to content

Support Operator Overloading LuaMetamethods in LuaObjects #104

Open

Description

@Foohy

I would love the ability to make a LuaObject class that can handle operators differently based on the type being passed in.
An example being a Vector class that you can choose to multiply by either another Vector, or a double scalar:

-- Vector = Vector * Vector
local vec = Vector(1, 2, 3) * Vector(3, 2, 1)

-- Vector = Vector * double
local vec2 = Vector(1, 2, 3) * 10.0

I'd imagine declaring such a set up could look something like this, where each different function signature all share the same LuaMetamethod attribute

  [LuaMetamethod(LuaObjectMetamethod.Mul)]
  public static LuaVector MulVecVec(LuaVector aVec, LuaVector bVec)
  {
      return new LuaVector(aVec.vector * bVec.vector);
  }

  [LuaMetamethod(LuaObjectMetamethod.Mul)]
  public static LuaVector MulVecFloat(LuaVector aVec, double scalar)
  {
      return new LuaVector(aVec.vector * scalar);
  }

But understandably right now it complains about there already being a 'Mul' metamethod declared.

I can work around the issue by declaring a single metamethod that accepts LuaValues as parameters, but that requires manually checking the type of each argument which is pretty tedious (especially when there's other similar overloads for other operators).

Thank you (and this library rules)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions