[Feature request] Computed properties

未关闭
#219 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
停滞
技术栈
lua
领域
compilers

调研方向

未指定任何仓库文件或测试。首先查看类编译路径以及 issue 中生成的 Lua 元表示例,然后定义 getter 和 setter 属性应如何与现有类行为交互。完成的标准是,所示的 YueScript 示例能够编译,并产生所描述的访问和赋值结果。

由索引模型根据 Issue 内容生成。

描述

Some languages allow defining methods that can be used like object attributes. For example, in JavaScript:

class Foo {
    get x() {
        console.log("Getting x");
        // In JS, member names starting wtih `#` are private and cannot
        // be accessed from the outside by any means.
        return this.#x;
    }

    set x(value) {
        console.log("Setting x to", value);
        this.#x = value;
    }
}

I would appreciate it if YueScript could do something similar. It could look something like this:

import "yue"

class Foo
    get x: () =>
        print("Getting x")
        @_x

    set x: (value) =>
        io.write("Setting x to ")
        yue.p(value)
        @_x = value

The output could then be something like this:

local yue = require("yue")
local Foo
local _class_0
local _base_0 = {}
local _index_0 = rawget(_base_0, "__index")
if _index_0 == nil then
	_index_0 = _base_0
end
if type(_index_0) == "table" then
	local _index_1 = _index_0
	_index_0 = function(_, key)
		return _index_1[key]
	end
end
local _newindex_0 = rawget(_base_0, "__newindex")
if _newindex_0 == nil then
	_newindex_0 = rawset
end
rawset(_base_0, "__index", function(self, key)
	local properties = rawget(_class_0, "__properties")
	local property = rawget(properties, key)
	if property ~= nil then
		local getter = rawget(property, "get")
		if getter ~= nil then
			return getter(self)
		end
	end
	return _index_0(self, key)
end)
rawset(_base_0, "__newindex", function(self, key, value)
	local properties = rawget(_class_0, "__properties")
	local property = rawget(properties, key)
	if property ~= nil then
		local setter = rawget(property, "set")
		if setter ~= nil then
			return setter(self, value)
		end
	end
	return _newindex_0(self, key, value)
end)
_class_0 = setmetatable({
    __init = function() end,
    __base = _base_0,
    __name = "Foo",
    __properties = {
        x = {
            get = function(self)
                print("Getting x")
                return self._x
            end,
            set = function(self, value)
                io.write("Setting x to ")
                yue.p(value)
                self._x = value
                return value
            end
        }
    }
}, {
    __index = _base_0,
    __call = function(cls, ...)
        local _self_0 = setmetatable({}, _base_0)
        cls.__init(_self_0, ...)
        return _self_0
    end
})
_base_0.__class = _class_0
Foo = _class_0
return _class_0

And to test it:

print(Foo, Foo.x)
local instance = Foo()
print(instance, instance.x)
instance.x = "Test"
print(instance.x)

Its output:

table: 0x12341234	nil
Getting x
table: 0xdeadbeef	nil
Setting x to "Test"
Getting x
Test
主要语言
Lua
星标
620
派生
45
平均合并
9 小时 5 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

IppClub/YueScript 的其他 Issue

查看 IppClub/YueScript 的全部 Issue

相似的 Issue

更多 Lua Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。