DIW

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

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

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

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.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New