mica-lang/mica

Make declarations order-independent in `impl` blocks

liquidev opened this issue · 0 comments

Currently constructors have to be declared first, for instance functions to be able to see fields:

impl struct Vector
  func x() @x end  # error

  func new(x, y) constructor
    @x = x
    @y = y
  end
end

I believe this restriction should be relaxed, such that constructors are processed before anything else, so that all other functions can see fields declared in constructors.


Update 2022-09-10

Additionally, I believe it would make sense to make field declarations part of the struct definition, such as struct Vector { @x, @y }. This would allow us to define mutability of fields in one central state rather than relying on order of constructors. The exact syntax is still to be determined.


Update 2022-01-29

I've determined that it would make the most sense in terms of implementation simplicity if we add field declarations right into impl blocks, using the new let syntax that is to be implemented (#129).

struct Vector impl
    let @x
    let @y

    func new(x, y) constructor = do
        @x = x
        @y = y
    end
end