Objective-C supports composition of types through various mechanisms, including categories and protocols. While it doesn’t have a direct equivalent to Go’s struct embedding, we can achieve similar functionality using these features.
In this Objective-C version:
We define a Base class with a num property and a describe method.
We define a Container class that has a Base object as a property, along with a str property.
We define a Describer protocol that declares the describe method.
We use a category to make Container conform to the Describer protocol, implementing the describe method by forwarding to the base object.
In the main function, we create a Container object, set its properties, and demonstrate accessing the composed Base object’s properties and methods.
We also show how the Container object can be used as a Describer.
To run this program, save it as StructEmbedding.m and compile it with:
This example demonstrates how Objective-C can achieve similar functionality to Go’s struct embedding using composition, categories, and protocols. While the syntax is different, the concepts of type composition and interface implementation are present in both languages.