JSAbrahams / mamba

๐Ÿ The Mamba programming language, because we care about safety
MIT License
88 stars 4 forks source link

Fields innaccessible in children classes #431

Closed JSAbrahams closed 1 year ago

JSAbrahams commented 1 year ago

Description of Bug

Certain fields are undefined when accessing children of classes.

How to Reproduce

type.mamba:

class MyGeneric: String
class MyType(def some_field: String)

type SomeState: MyClass when self.private_field > 2
type OtherState: MyClass when
    self.private_field  > 10
    self.private_field  < 200
    self.required_field < 50

type SuperInterface
    def bar: Int

type MyInterface: SuperInterface
    def required_field: Int
    def higher_order(self) -> int

# some class
class MyClass(def my_field: Int, other_field: String := "Hello"): MyType(other_field), MyInterface
    def required_field: Int := 100
    def private_field: Int := 20

    def fun_a(self: SomeState) => self.some_field := "my field is {self.required_field}"
    def fun_b(self) => print("this function is private: {self.private_field}!")

    def some_higher_order(self, fun: Int -> Int) -> Int => return fun(self.my_field)
    def higher_order(self) -> Int => return self.some_higher_order(\x: Int => x * 2)

Gives the following errors:

Error: Cannot infer type within call property. Expected a `self`.private_field, was Int
 โ”€โ”€โ†’ types.mamba:4:30
   4 | type SomeState: MyClass when self.private_field > 2
                                    ^^^^^^^^^^^^^^^^^^

We expect an Int, but MyInterface does not define private_field
 โ”€โ”€โ†’ types.mamba:4:30
   4 | type SomeState: MyClass when self.private_field > 2
                                    ^^^^^^^^^^^^^^^^^^
     โ””โ”€โ†’ In call property
       4 | type SomeState: MyClass when self.private_field > 2
                           ^^^^^^^

We expect a String, but MyInterface does not define some_field
 โ”€โ”€โ†’ types.mamba:22:35
  22 |     def fun_a(self: SomeState) => self.some_field := "my field is {self.required_field}"
                                         ^^^^^^^^^^^^^^^
     โ””โ”€โ†’ In call property
      22 |     def fun_a(self: SomeState) => self.some_field := "my field is {self.required_field}"
                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Cause

I expect that this is because the Context is not being properly created. Was noted in #416 However, because this is most likely an issue with the Context, will not solve there but created an issue. Perhaps because the check stage now works more properly we run into issues here.

JSAbrahams commented 1 year ago

Error was elsewhere.