MatijaL

MatijaL

Function which calculates time from now to end date - strange warning from VSCode

Hello,

I created a function which takes timedate as parameter in NaiveDateTime format. This date is an end date and the function has to calculate how much time is left from now to this end date.

def time_remaining(ends_at) when not is_nil(ends_at) do
    days_remaining = NaiveDateTime.diff(ends_at, NaiveDateTime.utc_now(), :day)

    if days_remaining < 1 do
      difference = NaiveDateTime.diff(ends_at, NaiveDateTime.utc_now(), :second)
      hours = div(difference, 3600)
      minutes = div(difference, 60) - (hours * 60)
      seconds = rem(difference, 60)

      "#{hours}h : #{minutes}min : #{seconds}s Remaining"
    else
      "#{days_remaining} Days Remaining"
    end
  end

The functions works as expected but VSCode is giving me this warning:

Guard test 
          _@46 ::
              {'safe',
               binary() |
               maybe_improper_list(binary() |
                                   maybe_improper_list(any(),
                                                       binary() | []) |
                                   byte(),
                                   binary() | [])} =:= 
          'nil' can never succeed

Can someone please help me locate the problem?

Marked As Solved

Sebb

Sebb

yes, @al2o3cr is right, didn’t look that closely.

You can try to delete the .elixir-ls folder and restart/reload vscode.
Sometimes there is a hickup.
And maybe install dilyxir and run mix dialyzer to get a better picture.

Also Liked

sodapopcan

sodapopcan

What version of Elixir are you using? I get no warnings when I compile in 1.14.1.

Regardless, since ends_at must be a NaiveDateTime, it’s better to be explicit about this in the function head as opposed to just checking that it isn’t nil:

def time_remaining(%NaiveDateTime{} = ends_at) do
  ...
end

or if you prefer when you can do the more verbose:

def time_remaining(ends_at) when is_struct(ends_at, NaiveDateTime) do
  ...
end
MatijaL

MatijaL

Deleting .elixir_ls folder solved it, thanks for taking the time to help me out here.

sbuttgereit

sbuttgereit

I haven’t seen this type of Dialyzer warning on a guard call before; but I have seen similar warnings for function parameters when Dialyzer thinks there’s no way for a given pattern match to possibly match a condition… which I sometimes do for a catch all function.

For example if I write something like the below, with no other calls to func_a/1:

def entry_func() do
  %{test: "Hi!"}
  |> func_a()
end

def func_a(%{} = param) do
  # do something
end

def func_a(_) do
  raise "Bad Param"
end

I will, with some regularity, see the ElixirLS Dialyzer warning that the second function head for func_a will never run… which looks like it could be similar to what you’re seeing. In this case Dialyzer isn’t exactly wrong… there’s no way for the second func_a/1 function head to match. But it’s not exactly helpful either because I may know now that in the future I might call it from somewhere else where the second function head is helpful.

Anyway, I can’t say for sure this is what you’re seeing, but it looks similar.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement