Our first example demonstrates interfaces, which are named collections of method signatures.
To run the program, save it as interfaces.nim and use the Nim compiler:
In this Nim example, we’ve implemented the concept of interfaces using Nim’s concept feature. The Geometry concept defines the required methods for any type that wants to be considered a geometric shape.
We then define Rect and Circle types and implement the required methods for each. The measure function demonstrates how we can use any type that satisfies the Geometry concept.
Nim’s approach to interfaces (concepts) is more flexible than Go’s, as it uses structural typing rather than nominal typing. This means that types don’t need to explicitly declare that they implement an interface; they just need to have the required methods.
To learn more about Nim’s concepts and how they compare to interfaces in other languages, check out the Nim documentation on concepts.