kip
"Adjusting" calling context of a function
Best explained by an example. Lets say I want to call:
Calendar.ISO.naive_datetime_to_string(year, month, day, hour, minute, second, microsecond)
From another calendar module that implements @behaviour Calendar. Calendar.ISO.naive_datetime_to_string calls time_to_string(hour, minute, second, microsecond) under the covers, resolved to be in the Calendar.ISO module.
But … it also calls date_to_string(year, month, day) which I’d like to be called as the version in my Calendar module.
I don’t think this is possible (and quite possible its even “bad”) but I’ve found a few situations where I’d like to delegate a function call to another module, but have the inner functions be resolved to my module. Behaviour-based modules is quite a good example of where this can crop up I think,
First Post!
peerreynders
What’s wrong with :
@impl true
def naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) do
date_to_string(year, month, day) <> " " <> Calendar.ISO.time_to_string(hour, minute, second, microsecond)
end
and use that instead of Calendar.ISO.naive_datetime_to_string? i.e. provide the alternate implementation where desired and only use the original implementation where needed.
In a way you seem to be looking for dynamic dispatch of a polymorphic operation typically implemented in class-based languages.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









