kuroko-lang / kuroko

Dialect of Python with explicit variable declaration and block scoping, with a lightweight and easy-to-embed bytecode compiler and interpreter.
https://kuroko-lang.github.io/
MIT License
427 stars 25 forks source link

[Question] Class field declaration? #31

Closed anzhi0708 closed 1 year ago

anzhi0708 commented 1 year ago
class A():
    let x = 233

This code won't work; can I ask why class fields must be declared without let?

Very cool project by the way ;-)

klange commented 1 year ago

Primarily, this was done to improve compatibility with Python code - it is possible to write a complete class implementation, including class fields, methods, and decorators, which is equally valid in both Python and Kuroko, with some restrictions. The way this was implemented involved parsing class bodies specially - field assignments are a unique syntax in the language and do not work like normal let declarations or even other assignment statements. In fact, in Python class bodies are normal function bodies, but in Kuroko class bodies must consist only of an optional initial docstring followed by field declarations and method definitions, possibly with decorators attached, but can not contain arbitrary statements or control flow structures.

anzhi0708 commented 1 year ago

This is VERY cool, it probably makes Python even more easy to understand, I love the scope design