JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

Use 'forward' keyword to forward methods #180

Open JSAbrahams opened 4 years ago

JSAbrahams commented 4 years ago

Current Issue

Currently, the forward keyword does nothing. Its intended behaviour, however, is to forward methods.

Method forwarding promotes composition (as opposed to inheritance).

High-level description of the feature

class InnerClass
    def inner_field: Int := 20
    def inner_method(): Int => 10

class MyClass
    def private inner_class: InnerClass := InnerClass() 
        forward inner_field, inner_method

The above would be equal to:

class InnerClass
    def inner_field: Int := 20
    def inner_method(): Int => 10

class MyClass
    def private inner_class: InnerClass := InnerClass()

    # We need a mechanism which writes to the inner field of the inner class if inner_field is mutable.
    def inner_field: Int := inner_class.inner_field
    def inner_method() -> Int => inner_class.inner_method()

Description of potential implementation

JSAbrahams commented 2 years ago

I believe that we can deal with the aforementioned problem by making use of Python's property built-in function, and generating the following:

class InnerClass
    def inner_field: Int := 20

class MyClass
    def private inner_class: InnerClass := InnerClass()

    @property
    def inner_field(self):
        return inner_class.inner_field

    @inner_field.setter
    def inner_field(self, new):
        inner_class.inner_field = new

Thus ensuring that when reassign to inner_field, we do actually assign to the field of the inner class directly. I.e.

my_class= MyClass()
print(my_class.inner_field) # prints 20
my_class.inner_field = 10
print(my_class.inner_field) # prints 10, because we assigned inner_field directly