vegabook
Pattern matching using [first | rest] in with clause seems to fail?
:digraph.add_edge gives unpredictable results when adding an edge, one only knows that the first element in the list will be :"$e"
I am trying to create an atomic :digraph.add_vertex+:digraph.add_edge+:dets.insert function. If any clauses in the with statement below fail, I need to roll back the previous ones. So if the last clause (:dets.insert) fails, I need to delete the vertex and the edge I just created. In order to do that, I need to destructure the return of the :digraph.add_edge function in the with clause, so that I can refer back to the created edge in the else part…
defp atomic_vedge({detspath, graph}, v, parent, meta, ts) do
# atomic add of a vertex and its edge to parent and save to dets
# if any stage fails preceding successes are rolled back
with {:addvertex, v} <- {:addvertex, :digraph.add_vertex(graph, v, meta)},
{:addedge, [:"$e" | rest]} <- {:addedge, :digraph.add_edge(graph, parent, v)},
{:insert, :ok} <-
{:insert, :dets.insert(detspath, {v, parent, ts, :vertedge, meta})} do
{:ok, {detspath, graph}}
else
{:addvertex, {:error, reason}} ->
{:error, reason}
{:addedge, {:error, reason}} ->
:digraph.del_vertex(graph, v)
{:error, reason}
{:insert, false} ->
:digraph.del_vertex(graph, v)
:digraph.del_edge(graph, [:"$e" | rest])
{:error, "dets vertex insert failed"}
end
end
However this is giving me an error when compiling:
Does this mean I cannot destructure into [first | rest] in a with statement? So I cannot track what the :digraph.add_edge returned in order to roll it back? It seems to be a problem with the [head | rest] part because the previous {:addvertex, v} doesn’t seem to cause a problem.
(the problem here btw is that :digraph is side-effecty. It doesn’t return a new graph each time because it uses ETS tables and that’s why I’m having to jump through these hoops)
Trending in Questions
Other Trending 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
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security














Marked As Solved
sabiwara
This is a scope problem:
restis not available in theelseclauses since it only gets defined when the pattern-matching succeeds.Since you only need it in the
insertcase, you could pass it in the tuple maybe?{:insert, rest, :dets.insert(...)}Regarding
v, it is available in the parent scope, which is why you’re not getting the error (but it might not be the value ofvyou expect). Here is a minimal example:PS: Complex
elseclauses inwithcan be considered an anti-pattern, maybe this code would actually be clearer with simply nestingcase? This way, the necessary variables from intermediate matches would be in scope.Also Liked
sabiwara
I agree that avoiding over-nesting and splitting in small functions is good advice in general.
Regarding what the exact maximum nesting level should be,
1feels quite opinionated. For example, it is quite easy to find examples that are nesting 2 or 3caseexpressions within the Elixir codebase.But like anything readability related, this is a very controversial topic, so I don’t think that there’s such a thing as a correct answer.
For the record, here is an opposite take embracing nesting over splitting functions (not necessarily agreeing but interesting to hear both sides).
adamu
Did you just tell the guy who wrote it to look it up?
Eiji
Elixirworks as expected. The bug is in your code. Firstly let’s take a look at the warning. As it says therestvariable is not used insidedo … elseblock. Secondly the value is assigned to variable only onsuccess, so it cannot be used outside ofdo … elseblock which contains instructions for success scenario. Having above in mind the error message is also correct as there is norestvariable assigned forelse … endblock which contains an instructions whenwithmatches fails.You can still access this data, but this is a bit tricky … See a simplified example below:
The above code obviously returns:
Last Post!
vegabook
yeah I’m gonna say 90% of Elixir devs know what Racket is.
BTW if I may say, this
~w[a b c]ais a complete code-golf gratuitous overcomplication which doesn’t even save a single keystroke. You should refrain from trying to look clever because it detracts from your otherwise decent points.Merry Christmas to you too!