mun icon indicating copy to clipboard operation
mun copied to clipboard

`impl` blocks for struct functions

Open Wodann opened this issue 2 years ago • 0 comments

Currently, Mun only allows functions to be defined independent from a struct. We want to be able to associate functions with a struct type, as such:

pub struct Foo {
  x: f32,
  y: f32
}

impl Foo {
  pub fn new(x: f32, y: f32) -> Self {
    Self { x, y }
  }

  pub fn set_x(&mut self, x: f32) {
    self.x = x;
  }
}

The above syntax is hypothetical. An important question to consider is whether we need to have a special case for struct(gc) vs struct(value).

Similar to Rust, impl blocks for member functions can be spread across multiple source files.

Definition of Done

  • [x] Parse impl blocks #551
  • [x] Parse self in function signatures https://github.com/mun-lang/mun/pull/564
  • [x] Handle associated functions in HIR #575
  • [ ] Handle methods in HIR #577
  • [ ] Support associated functions in LSP
  • [ ] Support methods in LSP
  • [ ] Code generation for method calls (an associated function is just a regular function)

Wodann avatar Jan 02 '23 19:01 Wodann