Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntactic sugar for shorthand getters and setters not respected #30

Closed
luttje opened this issue Jan 6, 2018 · 3 comments
Closed

Syntactic sugar for shorthand getters and setters not respected #30

luttje opened this issue Jan 6, 2018 · 3 comments

Comments

@luttje
Copy link

luttje commented Jan 6, 2018

Writing:
C#: public int SomeProp { get; set; }

Will not compile to anything in Lua, whilst the compiler normally makes fields for this purpose.

Workaround is manually making the fields:
C#:

private int someProp;

public int SomeProp { get => someProp; set => someProp = value; }
@yanghuan
Copy link
Owner

yanghuan commented Jan 7, 2018

write

using System;
namespace HelloLua {
    public static class Program {
        public static void Main() {
            A a = new A();
            a.SomeProp = 2;
        }
    }
    public sealed class A {
        public int SomeProp { get; set; }
    }
}

will be

-- Generated by CSharp.lua Compiler 1.1.0
local System = System
local HelloLua
System.usingDeclare(function (global) 
  HelloLua = global.HelloLua
end)
System.namespace("HelloLua", function (namespace) 
  namespace.class("Program", function (namespace) 
    local Main
    Main = function () 
      local a = HelloLua.A()
      a.SomeProp = 2
    end
    return {
      Main = Main
    }
  end)

  namespace.class("A", function (namespace) 
    return {
      SomeProp = 0
    }
  end)
end)

auto property will be convet to field, i think this is better, it is simple and performance.

@luttje
Copy link
Author

luttje commented Jan 7, 2018

Correct and again I'm sorry, the problem only occurs when the code is in a different assembly:

Write in HelloLua.csproj/Program.cs:

namespace HelloLua
{
    class Program
    {
        public static int MyProp { get; set; }

        static void Main(string[] args)
        {
            MyProp = 4321;
            TestLibrary.TestClass.SomeProp = 1234;
        }
    }
}

Write in TestLibrary.csproj:

namespace TestLibrary
{
    public static class TestClass
    {
        public static int SomeProp { get; set; }
    }
}

Compiles to:
HelloLua/Program.lua:

-- Generated by CSharp.lua Compiler 1.1.0
local System = System
local HelloLua
System.usingDeclare(function (global) 
  HelloLua = global.HelloLua
end)
System.namespace("HelloLua", function (namespace) 
  namespace.class("Program", function (namespace) 
    local Main, __staticCtor__
    __staticCtor__ = function (this) 
      this.MyProp = 0
    end
    Main = function (args) 
      HelloLua.Program.MyProp = 4321
      TestLibrary.TestClass.setSomeProp(1234)
    end
    return {
      Main = Main, 
      __staticCtor__ = __staticCtor__
    }
  end)
end)

and TestLibrary/TestClass.lua:

-- Generated by CSharp.lua Compiler 1.1.0
local System = System
System.namespace("TestLibrary", function (namespace) 
  namespace.class("TestClass", function (namespace) 
    local __staticCtor__
    __staticCtor__ = function (this) 
      this.SomeProp = 0
    end
    return {
      __staticCtor__ = __staticCtor__
    }
  end)
end)

The line TestLibrary.TestClass.setSomeProp(1234) wil error because no setSomeProp generated

@yanghuan
Copy link
Owner

yanghuan commented Jan 7, 2018

oh, i know what you mean, in this part, you must put all you code in one folder, then use the compile command, if you use dll reference, somethings will be different, because the code from dll, may be the true C# code, not lua, future may i will support csproj, like #17
but now you must put all code of what you want to convertd in one folder, then try compile...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants