Using Protocols to Create Polymorphic Functions
Elixir’s protocol is a feature that lets you create a single interface that various data types can implement. Using that, you can have polymorphism: a single interface that works with different data types. If you came from object-oriented languages like Java, you’ll see that it’s very similar to how interfaces work. Elixir protocols will help you create simple interfaces, leading to a better codebase design.
In this section we’ll explore more about structs, including structs that reference other structs. We’ll refactor our code to create a reusable module that shares functions between heroes and action selections. Then we’ll build polymorphic functions with protocols to display heroes and actions. ...
Get Learn Functional Programming with Elixir now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.