Define Types in Lua

One of the main features of this library is it's ability to create types in Lua and to have them be usable within C# code. You can derive from any C# type that is accessible according to LuaSettings. This supports overloads and implicit implementations.

To start, you need to define a new class, this is done with the new 'class' keyword. This library supports normal C# class definitions or the definitions for LuaBind.

-- define a function with name 'MyClass' that impliments IDisposable.
class MyClass : IDisposable

-- does the same thing.
-- note: the type does not need to be defined within Lua for it to have access to.
class "MyClass" (IDisposable)

After the class is defined, you define methods and fields. All members defined are public except for explicit interface definitions. First to define a field, set a member of the class to a value. If the value is a type (e.g. 'int') then there will be a field with the given name of the specified type. If a member in the base class has the same name, an exception is thrown as this library does not support hiding members. If the value set is a function, the member is a method. Otherwise the field is the type of the variable and that value is given statically as the default value.

-- define a field of type 'int'.
-- this is needed, without it the field will be of type 'double'.
MyClass.Field = int
MyClass.Field = 234

-- define a field of type 'LuaTable'.
MyClass.Table = {}

To define a method, use normal function definition syntax for defining a member function on the type.
WARNING If you forget the colon there will be NO error and will not define an argument of type 'self', see the Lua Documentation for more information.

function MyClass:Foo()
     print("success")
end

Implicit Implementation

If a base class or an interface defined a virtual property with the same name as a field, that is implemented (see below). This is how the code deals with different names, this works for properties and methods:

Base Class Interface Overloads Result
Defined not virtual/abstract * * Exception: Cannot hide member in base class.
Defined virtual/abstract * one Overrides that method.
Defined virtual/abstract * multiple Exception: Must specify overload.
none one one Implicitly implements it.
none one multiple Exception: Must specify overload.
none multiple * Nothing happens.

* This value is ignored.

This is for a virtual/abstract property. If you try to set the type of it, it will be ignored (if the types are compatible) or an exception will be thrown (if not). If you set a value to the property, it will create a get method that will always return that value and a set method that will throw a NotSupportedException. If the set method is not marked as virtual/abstract, it is ignored.

--this will bind to IDisposable.Dispose.
function MyClass:Dispose()
end

If any abstract members in the base class or members of an interface are not defined in Lua, a stub method is created that throws a NotImplimentedException. In the future, this may be changeable in LuaSettings.

If you define a method with the name '__ctor' it will be called in the constructor after default field initialization. Operators and events are currently not supported. If you call the type in Lua, it will throw an exception. If you try to add/change members after LuaClass.CreateType has been called in C#, it will do nothing and will continue silently.

Explicit Implementations

When a function with the same name is defined in two interfaces, none of them will be implemented by default. To explicitly implement a member, include the name of the interface in the definition of the method or field.

function MyClass.IDisposable:Dispose()
end

Last edited Sep 8, 2012 at 12:42 AM by ModMaker, version 1

Comments

No comments yet.