[Feature request] Computed properties

Aperta
#219 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
lua
Ambito
compilers

Direzione di ricerca

Non è indicato alcun file o test del repository. Inizia esaminando il percorso di compilazione delle classi e l’esempio di metatable Lua generato mostrato nell’issue, quindi definisci come le proprietà getter e setter debbano interagire con il comportamento esistente delle classi. Il lavoro è completato quando l’esempio YueScript mostrato viene compilato e produce i risultati di accesso e assegnazione descritti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Lua
Stelle
620
Fork
45
Merge medio
9h 5m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di IppClub/YueScript

Tutte le issue di IppClub/YueScript

Issue simili

Altre issue su Lua

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.