rubycrystal-lang

interfaces in Crystal


Is there a possibility to use the concept of interfaces in the Crystal programming language ?

What comes near is modules, but it seems that they are mostly used (and designed) for namespace handling ?

So the question goes more towards Golang style of interfaces.


Solution

  • Modules are used to create a namespace hierarchy, but this isn't what they are used for in the first place (nested classes also create a hierarchy).

    Modules are meant as mixins, to group methods whose implementation can be shared in different classes (or modules). Modules can also hold abstract methods, whose implementation is specific.

    Modules are explicitly included into classes, and you can restrict types on the module name, so they then acts as an interface.

    A pure interface is possible, if you create a module that only has abstract methods.