-
-
Save sasa1977/346f9c636f8536a89f665ab1246e7580 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Person.Base do | |
| defmacro __using__(overrides) do | |
| quote bind_quoted: [overrides: overrides] do | |
| base_fields = [name: "", age: 0] | |
| fields = Keyword.merge(base_fields, overrides) | |
| defstruct fields | |
| def greet(person) do | |
| "Hello, I'm #{person.name} and I'm #{person.age} years old" | |
| end | |
| def adult?(person) do | |
| person.age >= 18 | |
| end | |
| defoverridable greet: 1, adult?: 1 | |
| end | |
| end | |
| end | |
| defmodule Person do | |
| use Person.Base | |
| end | |
| defmodule Employee do | |
| use Person.Base, | |
| salary: 0, | |
| department: "" | |
| def greet(employee) do | |
| "Hi, I'm #{employee.name}, I work in #{employee.department}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment