Use static class/method/property or not ?

Mar 31, 2014 at 4:50 PM
What is your recommendation/best solution on the use of static class/method/property or not?
  public class Test
  {
     public readonly string Prop = "123";
     public int Meth(int val) { return val + 1; }
  }

  public static class StaticTest 
  // OR
  public class StaticTest 
  {
     public static readonly string PropStatic= "123";
     public static void MethStatic(int val) { return val + 1; }
  }

  // Use Static class/method/property
  using (Lua l = new Lua()) 
  {
     LuaGlobal lg = l.CreateEnvironment();
     lg.RegisterPackage("StaticTest", typeof(StaticTest));
     // OR
    dynamic g = lg;
    g.PropStatic = StaticTest.PropStatic;
    g.MethStatic= new Action<int>(StaticTest.MethStatic);
    // OR Not use Static class/method/property?
  }
Coordinator
Mar 31, 2014 at 8:10 PM
For static or non static classes the same rules apply as in c#/.net. There is no difference in the implementation.

If the class only has methods or readonly field/constants than it is better to use a static classes. If the class has own fields, that are changed through the methods, within the class it is better to use a instance/object. You have one object more in the memory. The type description and binding rules are in both cases shared over all LuaGlobals of one Lua-Script-Engine. So, the runtime overhead should be the same.

In your example a static class is the best solution.

But I would create the package of the static class in this way:
g.StaticText = LuaType.GetType(typeof(StaticTest));
The object like this:
g.Test = new Test();
Create the the values over properties in the LuaGlobal/LuaTable is the most memory consuming. First you create a dynamic field in the LuaTable. Secound, you create a more complex binding rule. Schematic:
 not table.IsChanged ? values[memberIndex] : UpdateDynamicBind()
Btw. it is better to override the LuaGlobal for this scenario.
public class MyLuaGlobal : LuaGlobel
{
  [LuaMember("StaticTest")]
  private LuaType StaticTest {get{return LuaType.GetType(typeof(StaticTest);}}

  [LuaMember("PropStatic")]
  private string PropStatic {get{return "123";}}
}
This will generate something like this in the Lua script (only when the member is used )
(not g.IsChanged ? g.values[memberIndex] : UpdateDynamicBind()) ?? g.PropStatic
Becareful, PropStatic is a kind of changable or overridable.
Apr 1, 2014 at 5:37 AM
OK.

But how to use MyLuaGlobal? Is it well : dynamic g = new MyLuaGlobal();

What do you mean by "Becareful, PropStatic is a kind of changable or overridable."
PropStatic is changable because MyLuaGlobal is overridable?
Coordinator
Apr 1, 2014 at 9:14 AM
Edited Apr 1, 2014 at 9:16 AM
Near:
public class MyLuaGlobal : LuaGlobal
{
  public MyLuaGlobal(Lua lua)
    : base(lua)
  {
  }
}

using(Lua l = new Lua())
{
  dynamic g = new MyLuaGlobal(l);
I mean, that you can override these members in the script.
print(PropStatic); // prints 123
PropStatic = "4";
print(PropStatic); // prints 4
PropStatic = null;
print(PropStatic); // prints 123
btw.
StaticTest.PropStatic = "4";
should raise a exception.
Marked as answer by PapyRef on 4/3/2014 at 8:43 AM
Apr 1, 2014 at 12:24 PM
Edited Apr 1, 2014 at 12:25 PM
hummm, ..

[LuaMember("PropStatic")] => LuaMember is not found.
private LuaType StaticTest => LuaType is not found.

IntelliSense gives no information about LuaMember & LuaType.
public class MyLuaGlobal : LuaGlobal
{
  public MyLuaGlobal(Lua lua)
    : base(lua)
  {
  }

  [LuaMember("StaticTest")]
  private LuaType StaticTest {get{return LuaType.GetType(typeof(StaticTest);}}

  [LuaMember("PropStatic")]
  private string PropStatic {get{return "123";}}
}
Coordinator
Apr 1, 2014 at 2:03 PM
Do you use 0.8.4?




You missed a ) in private LuaType StaticTest {get{return LuaType.GetType(typeof(StaticTest);}}.
Apr 1, 2014 at 4:21 PM
No 0.8.2
I did the update via Nuget. It's OK ;)