DIW
Cannot invoke remote function :erlang.length/1 inside match
I’m trying to take a float, add it to the array of floats in a model instance, and save it back to the database. I only want the array to have a max length of 90. Here is my code for that so far.
def add_price(model, %{"price" => price}) do
current_price = model.price
price_list =
case current_price do
nil -> [price]
90 > length(current_price) -> [current_price | price]
_ ->
prices = List.delete_at(current_price, 0)
[prices | price]
end
model.price = price_list
end
I’ve tried doing this a few ways, but I keep getting the error “Cannot invoke remote function :erlang.length/1 inside match.” How do I go about checking the length?
Marked As Solved
Nicd
You cannot run length/1 inside a pattern match. You should move it to a guard:
list when length(list) < 90 -> [list | price]
Also Liked
NobbZ
Because only a limited set of functions is callable in a guard, those are called guardsafe functions. This is a limitation of the BEAM to make some optimisations possible.
Basically it is, because the BEAM weren’t allowed to reorder clauses if functions in a guard were allowed to have side effects.
Last Post!
NobbZ
Because only a limited set of functions is callable in a guard, those are called guardsafe functions. This is a limitation of the BEAM to make some optimisations possible.
Basically it is, because the BEAM weren’t allowed to reorder clauses if functions in a guard were allowed to have side effects.
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









