ballerinaballerina-swan-lake

What is the best practice for sharing records among modules in Ballerina?


I have a Ballerina package consisting of multiple modules, each requiring the same set of record types. Unfortunately, it seems impossible to define these common record types in the main module and have the sub-modules inherit them. The only solution I can think of is creating a separate module exclusively for the shared record types and importing it into all other modules. However, this approach results in the "types" module containing only a list of records without any functionality. Could you confirm if this is the appropriate method or suggest a better design pattern for this scenario?


Solution

  • In Ballerina, there's currently no other way to share record types across multiple modules other than defining a library module for those record types and importing it in the modules that need to use them. There is no concept of inheritance in Ballerina, so record types cannot be inherited from one module to another.

    The best approach is to create a library module that defines the shared record types, and then import it into the other modules that need to use those types.