denna
I am trying to provide a way to create something like inheritance.
i want to be able to call a a function on a Module that inherits the function from another Module. Is the following the most straightforward way?
defprotocol Proto do
@spec foo(t()) :: charlist()
def foo(item)
end
defmodule SpecializedProto do
@callback info() :: charlist()
end
defimpl Proto, for: Atom do
@impl true
def foo(item) do
apply(item,:foo,[item])
end
end
defimpl Proto, for: SpecializedProto do
@impl true
def foo(item) do
apply(item,:info,[])
end
end
defmodule Test do
@behaviour SpecializedProto
def info(), do: "test"
defdelegate foo(x), to: Proto.SpecializedProto
end
Proto.foo(Test)
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
I believe you should start with describing the problem you are trying to solve, so we can give you ideas and alternative approaches. Inheritance is just one way to do things. There are others as well.
denna
hm difficult to break down, but the library I am working on is trying to provide abstractions for embedded UI. Combinations of button presses tigger actions and combinations of leds. so i am trying to create a widget library that abstract different kind of behaviors.
it works ok well with only functions but then i need to keep track of the state initialization separately. So instead of composing functions i would rather compose very specialized modules (Test in my example) that implement a protocool (Proto) that specifies an additional init function, but then the modules use again to more generalized functions (SpecializedProto in my example).
al2o3cr
This isn’t what protocols are for, and probably (I have not run it) doesn’t work like you really want.
For the above declaration to make sense, you’d need to be calling
Proto.foowith a%SpecializedProto{}struct.Protocols solve a particular problem: how to make a function handle many data types that aren’t known by the function’s original authors. A simple example from the stdlib is
String.Chars, which definesto_stringand is used by the kernelto_stringfunction.Without protocols, the only way to write
String.Chars.to_stringwould be with many heads:However, having custom formatting for user-defined structs would require adding new clauses to
String.Chars.to_stringwhich isn’t supported (or particularly practical).Protocols let you do that kind of dispatching because
defimpls don’t have to be supplied all-at-once likedefs.A general suggestion when prototyping something like this: start out by writing a straightforward implementation that does what you want and uses the components you’re creating. Copy-paste code FREELY during this initial part, but try to keep changes to copied code clearly labeled.
Once you’ve got things sketched out enough, only then start thinking about how to DRY things out / reduce boilerplate / do macro-fu / etc. It will be a lot more obvious where you need flexibility like protocols with working code.
denna
it does work because Test is a atom. so it uses:
so it would be in this case what is in oo speak a singleton
i realized i maybe simplified my example to much so now it want it to be possible with a struct or a Modul-Atom in the call:
what is not nice is the use of apply and defdelegate because the implementation is positioned in another module again.
al2o3cr
Your example is more readable if some of the reuse of the name
SpecializedProtois removed (here are the changed clauses):I’m still not understanding the intent of passing an atom versus a struct in these functions, but I suspect that’s because that argument never gets used.
For instance, think about the path that a call like
SpecializedProto.info(Test)takes:Atomimplementation, callsTest.info(Test)Test.info(Test)delegates toSpecializedProto.Test.info(Test)SpecializedProtoimplementation forTeststructs then receives the atomTestinstead of a structPerhaps something like this would be better:
Then you don’t need the
defdelegate info(...)onTest, andSpecializedProto.Testalways gets the struct that it expects.