Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/typesend/prototype
An Elixir library that implements prototype inheritance for dynamic function calling
https://github.com/typesend/prototype
Last synced: 8 days ago
JSON representation
An Elixir library that implements prototype inheritance for dynamic function calling
- Host: GitHub
- URL: https://github.com/typesend/prototype
- Owner: typesend
- Created: 2024-09-10T21:58:49.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-11T23:44:46.000Z (2 months ago)
- Last Synced: 2024-09-16T01:05:14.630Z (2 months ago)
- Language: Elixir
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Prototype
An Elixir library that implements prototype inheritance for dynamic function calling
over a chain of modules. These prototypes can be user-defined struct modules
or any other modules.## Usage
To specify a module's prototype module, implement the `__prototype__/0` function like so:
```Elixir
defmodule YourModule do
def __prototype__, do: AnyOtherModule
end
```Then to call functions wherever they are found up the prototype chain
you might:```Elixir
case Prototype.apply(YourModule, :function_name, ["argument", "list"]) do
{:ok, return_value, _meta} -> return_value,
{:missing_fun, _function_name, _args, _meta} -> {:error, "function not found"}
end
```Where `_meta` contains tracing metadata about the call chain that executed.
As you might suspect, if the function cannot be found in any of the modules
then the result is a :missing_fun tuple because it's not very fun to have
multiple attempts to call a function fail!## Installation
This package can be installed by adding `prototype` to
your list of dependencies in `mix.exs`:```elixir
def deps do
[
{:prototype, "~> 1.0.0-alpha"}
]
end
```## Feedback welcome!
If you use this library and have suggestions for how to make it better,
please [file an issue](https://github.com/typesend/prototype/issues).