vezel-dev / celerity

An expressive programming language for writing concurrent and maintainable software.
https://docs.vezel.dev/celerity
BSD Zero Clause License
9 stars 1 forks source link

Language idea: Friend modules #23

Open alexrp opened 1 year ago

alexrp commented 1 year ago

A module A can declare that module B is a friend and so is allowed to access private members of A. The keyword is already reserved.

Something like this:

a.cel:

mod {
    friend B;

    fn foo() {
        42;
    }
}

b.cel:

mod {
    fn bar() {
        A.foo(); // OK; no panic.
    }
}

For this to work, the semantics of a field expression (. operator) would be changed to pass along the accessing module when looking up the member. The runtime would then check if the resolved module declares the accessing module as a friend.

This sounds inefficient, but I think object shapes and basic block versioning based on types would allow us to fully specialize most such cases. This feature can only realistically be prototyped and considered once we have a runtime capable of such optimizations.

This is tentatively approved for 2.0, pending prototyping.

alexrp commented 1 year ago

Having thought through the design of basic block versioning and shapes in the runtime, I'm now completely confident this can be implemented in a performant way.

That said, there are still some design issues here that I want to think through. For example, what if a module A declares friend B - is it also a friend of B::C?