krzema12 / kotlin-python

Python target for the Kotlin Programming Language. See https://github.com/krzema12/kotlin-python/tree/python-backend/python
https://discuss.kotlinlang.org/t/idea-python-backend/19852
48 stars 1 forks source link

Support static fields #27

Closed SerVB closed 3 years ago

SerVB commented 3 years ago

After working a bit with names clashes (cherry picked 2c1f1766ffee677b4080f2fdfd8431df534a8936), I've realized that many errors come from missing global declarations. I've dropped the cherry picked changes and implemented globals. Unfortunately, I've realized very late that you've done similar work in #12. Anyway, I've compared and I guess that my implementation is a bit more concise since no globals are generated for read access. For example, for the following example, global is optional, so not generated in my PR:

Companion_instance = None
def Companion_getInstance():
    global Companion_instance
    if Companion_instance == None:
        Companion()

    return Companion_instance

Also, these changes make much tests pass despite we still have name clashes: the Companion_instance is overwritten right before usage by the correct object.

So I propose merging these changes. It's an atomic, self-contained change.

SerVB commented 3 years ago

TODO: merge #29 firstly, then rebase this PR to the updated default branch. [v] Done

SerVB commented 3 years ago

I've just found a way to fix the problem for all known static fields. So I've decided to change it in this PR since it's more logical.

+17 additional box tests passing