How to put a if else in a function

i want to add a condition in the add_entry to limit the input such like if else in java, any help

server.ex

def add_entry(todo_server, new_entry) do
    GenServer.cast(todo_server, {:add_entry, new_entry})
  end
@impl GenServer
  def handle_cast({:add_entry, new_entry}, {name, todo_list}) do
    new_list = Todo.List.add_entry(todo_list, new_entry)
    Todo.Database.store(name, new_list)
    {:noreply, {name, new_list}}
  end

list.ex

def add_entry(todo_list, entry) do
    entry = Map.put(entry, :id, todo_list.auto_id)
    new_entries = Map.put(todo_list.entries, todo_list.auto_id, entry)

    %Todo.List{todo_list | entries: new_entries, auto_id: todo_list.auto_id + 1}
  end

:wave:

Check out https://elixir-lang.org/getting-started/case-cond-and-if.html

Depending on what you are trying to achieve, having multiple function heads for the add_entry function is idiomatic.

for example:

def add_entry(todo_list, %{thing: thing}=entry) when thing=="don't add me", do: todo_list

def add_entry(todo_list, %{}=entry)  do
 # what you had before
end

Alternatively, if you really want to use if, remember that it returns a value and you can’t modify variables with scope external to it, so you have to do something like:

  new_entries = if <some criteria> do
    Map.put(todo_list.entries, ...)
  else
    todo_list.entries # return the unaltered map if criteria don't apply
  end

If you want to spell out a bit more what you are trying to do we could give you the best option…

Todo.Server.add_entry(test, %{date: ~D[2020-05-19], time: ~T[16:52:00], title: "Shopping"})
this is what the input and I want to set it if any of the value is nil or empty it return an IO statement " input cannot be empty and return to the same iex

so follow the idea you gave is it like this?

def add_entry(todo_list, %{entries: entries}=entry) when entries  ==" ", do: IO.puts("input cannot be empty!")
end

def add_entry(todo_list, %{}=entry)  do
    entry = Map.put(entry, :id, todo_list.auto_id)
    new_entries = Map.put(todo_list.entries, todo_list.auto_id, entry)

    %Todo.List{todo_list | entries: new_entries, auto_id: todo_list.auto_id + 1}
end

Close…

def add_entry(todo_list, %{entries: entries}=entry) when entries  =="", do: todo_list

or…

def add_entry(todo_list, %{entries: entries}=entry) when entries  ==" "  do
  IO.puts("input cannot be empty!")
  todo_list # Return the unaltered input so the calling code is consistent
end

But the question here is: do you want to raise an exception, or return an erroneous value to the caller? If exception is fine, you can just pattern match the happy path like this:

def add_entry(list, %{entries: text}) when text != "" do
  # do stuff with valid input
end

Then, if the function is called with an empty entries key the runtime will just raise FunctionClauseError because there is no function that handles the empty value.

what if I want to return an erroneous value?is that I need to set up an error statement by myself?

If your map contains keys like :date, :time, etc., then your code above won’t work (because it matches an entry with key :entries).

There are a few possible ways to solve this. First, you could check if any of the entry fields is blank. Remember that maps are Enumerable, so you can enumerate them as a collection of {key, value} with the Enum module:

def add_entry(todo_server, new_entry) do
  if Enum.any?(new_entry, fn {_key, value} -> value == nil || value == "" end) do
    IO.puts("input cannot be empty!")
  else
    GenServer.cast(todo_server, {:add_entry, new_entry})
  end
end

Instead of printing an output though, it would be better to return an error, so the caller can pattern match easily. Usually, one would return :ok or {:error, reason}:

def add_entry(todo_server, new_entry) do
  if Enum.any?(new_entry, fn {_key, value} -> value == nil || value == "" end) do
    {:error, "input cannot be empty!"}
  else
    GenServer.cast(todo_server, {:add_entry, new_entry})
  end
end

If the entries always have the same keys, there is a possibly better way to do this: you can use a struct for the entry, instead of a map, to enforce the “shape” of the entry:

defmodule Todo.Server do
  defmodule Entry do
    @enforce_keys [:date, :time, :title]
    defstruct [:date, :time, :title]
  end

  def add_entry(todo_server, %Entry{date: date, time: time, title: title})
  when is_nil(date) or is_nil(time) or is_nil(title) or title == "" do
    {:error, "input cannot be empty!"}
  end

  def add_entry(todo_server, new_entry = %Entry{}) do
    GenServer.cast(todo_server, {:add_entry, new_entry})
  end

  def add_entry(_server, _entry), do: {:error, "Invalid input"}
end

This way, the entry is now a struct that enforces that all of :date, :time, and :value are present. This also mean, though, that the caller of the add_entry function has to pass an Entry struct instead of a map, so it’s your choice whether this is desirable or not.

P.S.:
Unrelated to your question, but you might want to use GenServer.call/3 instead of GenServer.cast/2, even if you don’t need a result. The reason is explained here: https://elixir-lang.org/getting-started/mix-otp/genserver.html#call-cast-or-info

3 Likes

Show us what you have tried to make the function return an error value?

so if i not enter both value there will having a crash appear


so i want to make it limited at the input part to stop this happening

Why is your repo not a normal Mix project, created with mix new ...?

i have no idea :sweat_smile: my teacher just give a base code like this one and ask us to make it efficiency/ capacity and fault tolerance, that why i have the idea to make it unable put empty and werid value at the beginning.

def add_entry(_server, _entry), do: {:error, "Invalid input"}
  end

what is this line is about? before that three i have some idea what they are doing but this line seems like is doesn’t do anything?

It is there just to return an {:error, cause} in case the entry is not an %Entry{} struct (for example, if the caller does something like add_entry(pid, nil)).

You might remove it, and in that case calling add_entry with something that is not an %Entry{} would raise an error (no function clause matching the input) instead of returning {:error, "Invalid input"}.

Both approaches could be reasonable, depending on the situation. If calling add_entry with something of the wrong type is not an expected situation, you can remove that line.

is i input the wrong value?
if i put this line
def add_entry(_server, _entry), do: {:error, "Invalid input"}
is return the “Invalid input”
and if i run without it it become like this

Yes, with your current code the entry must be an %Entry{} struct instead of a plain map, so you should call add_entry like this:

Todo.Server.add_entry(pid, %Todo.Server.Entry{date: ~D[2020-05-19], time: ~T[16:52:00], title: "Shopping"})

If you don’t want to type the whole %Todo.Server.Entry you can alias it:

alias Todo.Server.Entry

Todo.Server.add_entry(pid, %Entry{date: ~D[2020-05-19], time: ~T[16:52:00], title: "Shopping"})

Got it,seems like working fine, thanks for your help :smiling_face_with_three_hearts: :vulcan_salute:

iex(7)>Todo.Server.add_entry(testa, %Todo.Server.Entry{date: ~D[2020-05-19], time: [], title: "Shopping"})
:ok
iex(8)>Todo.Server.add_entry(testa, %Todo.Server.Entry{date: ~D[2020-05-19], title: "Shopping"})
**(ArgumentError) the following keys must also be given when building struct Todo.Server.Entry: [:time]
(todo) expanding struct: Todo.Server.Entry._struct_/1
iex(8)>Todo.Server.add_entry(testa, %Todo.Server.Entry{date: ~D[2020-05-19], time: ~T[], title: "Shopping"})
**(ArgumentError)  cannot parse "" as time, reason: :invalid_format
(elixir)lib/calendar/time.ex:266: Time.from_iso8601~/2
(elixir) expanding macro: Kernel.sigil_T/2
iex:8: (file)
iex(8)>Todo.Server.add_entry(testa, %Todo.Server.Entry{date: ~D[2020-05-19], time: , title: "Shopping"})
**(SyntaxError) iex:8: syntax error before: ','

so i trying on other value to create the situation that is return “input cannot be be empty”, but is seems like the other error message will show up before the message


so i trying on other value to create the situation that is return “input cannot be be empty”, but is seems like the other error message will show up before the message