Mercerenies / gdlisp

Lisp on the Godot platform
GNU General Public License v3.0
141 stars 1 forks source link

Inner Class References on Static Methods #47

Open Mercerenies opened 3 years ago

Mercerenies commented 3 years ago

If we reference the enclosing class in a static method, we may load the class several times.

(defn foo ())
(defclass A (Reference) (defn bar () static (foo) (foo)))

This compiles to

extends Node
static func foo():
    return null
class A extends Reference:
    func _init():
        pass
    static func bar():
        load("res://REPL.gd").foo()
        return load("res://REPL.gd").foo()
static func run():
    return null

I propose that the static func bar part should instead be

static func bar():
    var tmp = load("res://REPL.gd")
    tmp.foo()
    return tmp.foo()

We already optimize self-load on non-static methods by making an instance variable (see #30). It makes sense to do it in the static case as well with a local variable.