artem
Hi all
I am still in my early days with Elixir and relatively new to functional languages (did one hobby project in Elm), so most interested in figuring the elixir ways for this or that.
Quite often I am in the situation where I need to have a look at the function input and do an early exit, yet an input validation is something beyond what simple pattern matching or guard could do - a function needs to be called.
For example, in my weather forecast live view there is a handle_event that should only go fetch weather if user has chosen a date already. Otherwise, we just do nothing:
def handle_event("fetch-weather", _, socket) do
weatherRow = if (socket.assigns[:target_date] == nil) do
[]
else
# actually fill weatherRow with some sensible info
end
socket = assign(socket,
:weatherRow, weatherRow
)
{:noreply, socket}
end
All these argument validation feel to not nice in elixir, but I can’t really think of a good solution.
The best I could think of is about making an original function just extract the data to be validated:
def handle_event("fetch-weather", _, socket) do
target_date = socket.assigns[:target_date]
weatherRow = target_date |> handle_fetch_weather_inner
socket = assign(socket,
:weatherRow, weatherRow
)
{:noreply, socket}
end
defp handle_fetch_weather_inner(nil)
[]
end
defp handle_fetch_weather_inner(nonEmptyTargetDate)
# Fetch weather using the target date
end
How would you do something like this?
Am I missing some way to pattern match or put guard on the original handle_event?
Trending in Questions
Other Trending Topics
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
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stefanluptak
Hi @artem, welcome to the forum.
I sometimes want a simple
return ...call too. I was used to having that in in Swift.Anyway, here’s a gist I made with a few approaches. Somebody will come up with an even more clever one, I guess. But hopefully, this would be useful for you.
If you have a LiveBook, you can open that file and play with it more.
al2o3cr
IMO @stefanluptak’s first approach (top-level
ifwith return values in each branch) is pretty standard.Alternatively, consider interlocking the code so that whatever is sending
fetch-weathercan’t happen withouttarget_datebeing in the assigns.ityonemo
Totally different way:
Note
List.wrapgives[]onnil. It’s a secret power weapon in elixir.is also a common pattern in my toolbelt
artem
Thank you guys, I didn’t realize I could pattern match on map inside a map as
I guess most proper approach is what @stefanluptak posted as handle_event1, for now I’ll try some more function header matching to learn it more. It probably wouldn’t work if I needed some checks with function calls (e.g.
validate_date(target_date)), but for my current needs matching works well.I figured I can even check for the existence of a key as well as in