Search code examples
crystal-lang

In Crystal, what's the difference between inheritance and inclusion?


In most of the Crystal docs, class inheritance is used, with < syntax (e.g. https://stackoverflow.com/a/61053311/2954547).

However, HTTP::Handler says that custom handlers must include the HTTP::Handler module and not inherit from some class.

I can't find a description in the Crystal docs of what include-ing a module is supposed to do, or how it differs from <-inheritance of classes.

What does it mean when a class includes a module?


Solution

  • Inclusion is also a form of inheritance.

    The main difference is really that extending a type is limited to exactly one parent. The extension inheritance graph of the entire program is a tree. In contrast, a type can include multiple modules. And there can be multiple include inheritance paths between two types.