Search code examples
dateerlangguard

Using dates in guard statements of an erlang function


I have defined a function as below

bc_link_for(Date) when Date < {2010, 1, 4} orelse Date > erlang:date()
    -> "";
bc_link_for(_)
    -> "something".

The second guard statement is for future dates. When I comile this program I get the error illegal guard expression. There is not much help available online around using date types in guard statements.


Solution

  • Functions are generally not allowed in guards, except for the specific guard functions like is_integer, is_float, is_list, length, etc.

    But in your case, erlang:date() is what's throwing the error.

    You'll just need to do the comparison inside the function:

    bc_link_for(Date) ->
        case Date < {2010, 1, 4} orelse Date > erlang:date() of
            true -> "";
            false -> "something"
        end.
    

    Using a tuple in a comparison is perfectly acceptable in guards.