is_time, is_date and is_naive_datetime macros ?

1,842 views
Skip to first unread message

João Bernardo

unread,
Oct 25, 2016, 3:29:58 PM10/25/16
to elixir-lang-core
Hi !

Why aren't there is_time, is_date and is_naive_datetime guards ?

For sure I am not the first one to propose this, and baybe there is a reason for that.

But I believe this would be really useful.

Thanks.

José Valim

unread,
Oct 25, 2016, 3:34:35 PM10/25/16
to elixir-l...@googlegroups.com
Unfortunately we cannot check structs on guards. Fortunately we can pattern match on them:

    def my_function(%Time{}) do ...
    def my_function(%Date{}) do ...

etc.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/38581c82-efae-4775-a8f5-e01a89ec6290%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

João Bernardo

unread,
Oct 25, 2016, 3:51:37 PM10/25/16
to elixir-lang-core, jose....@plataformatec.com.br
Hum... I am new to elixir, why can't we check structs on guards ? Writing a macro to check a struct seems easy.

Nonetheless, pattern match does the job.

terça-feira, 25 de Outubro de 2016 às 20:34:35 UTC+1, José Valim escreveu:
Unfortunately we cannot check structs on guards. Fortunately we can pattern match on them:

    def my_function(%Time{}) do ...
    def my_function(%Date{}) do ...

etc.



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Tue, Oct 25, 2016 at 9:29 PM, João Bernardo <jb.am...@gmail.com> wrote:
Hi !

Why aren't there is_time, is_date and is_naive_datetime guards ?

For sure I am not the first one to propose this, and baybe there is a reason for that.

But I believe this would be really useful.

Thanks.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Onorio Catenacci

unread,
Oct 25, 2016, 4:02:09 PM10/25/16
to elixir-lang-core
It's a limitation of the underlying BEAM machine. Only certain expressions are valid in guards.

From this page: http://erlang.org/doc/reference_manual/expressions.html#id2273702

"The reason for restricting the set of valid expressions is that evaluation of a guard expression must be guaranteed to be free of side effects."

If there is a way to guarantee that any given macro is certain to be free of side-effects then it could be supported although it'd require a change to the underlying BEAM itself.

--
Onorio

João Bernardo

unread,
Oct 25, 2016, 4:11:43 PM10/25/16
to elixir-lang-core
Makes sense.

Thanks for the answer !

Vasilis Spilka

unread,
Apr 15, 2017, 7:08:02 AM4/15/17
to elixir-lang-core
While we cannot guard directly on the struct we can `hack` it a bit and do something like this

```
@dates [NaiveDateTime, DateTime]

def funky(%{__struct__: struct} = map) when struct in @dates, do: IO.puts "I am some date :D !"
```
Reply all
Reply to author
Forward
0 new messages