Check if a project has a deadline set, and warn if it is in the past.

← Control Flow · Ref: Q1201

Use an if guard to declare the deadline variable and check isSet in one expression. If the function returns an unset Date, the body is skipped. Inside the guard, compare the deadline against today.

GUARD WITH COMPARISON

  if deadline <- findDeadline() with deadline < today
    stdout.println("Deadline has passed!")

The 'with' clause adds a condition to the guard: the body runs only if the value is set AND the condition is true. This replaces null checks and isBefore() in one expression.

See Q1196 for guard with date. See Q74 for guard if. See Q1186 for date comparison.

Example

defines module qa.flow.guard.deadlinecheck

  defines function

    findDeadline()
      <- rtn <- Date()

    findActiveDeadline()
      <- rtn <- 2024-03-01

    findFutureDeadline()
      <- rtn <- 2025-12-31

  defines program

    GuardDeadlineCheckDemo()
      stdout <- Stdout()

      today <- 2024-06-15

      // === GUARD: NO DEADLINE SET ===

      if deadline <- findDeadline()
        stdout.println(`Deadline: ${deadline}`)
      else
        stdout.println("No deadline set for this project")

      // === GUARD: DEADLINE IN THE PAST ===

      if deadline <- findActiveDeadline()
        if deadline < today
          stdout.println(`Warning: deadline ${deadline} is in the past`)
        else
          stdout.println(`Deadline ${deadline} is still upcoming`)

      // === GUARD: DEADLINE IN THE FUTURE ===

      if deadline <- findFutureDeadline()
        if deadline > today
          stdout.println(`On track: deadline ${deadline} is in the future`)

      // === MULTIPLE DEADLINE CHECKS ===

      if first <- findActiveDeadline()
        if second <- findFutureDeadline()
          gap <- second - first
          stdout.println(`Gap between deadlines: ${gap}`)

Common mistakes

E01072 — EK9 has no null and no isBefore() method. Use guard variables in if statements and comparison operators on Date. See ek9 -h E01072 for details.

Incorrect:

deadline <- findDeadline()
      if deadline != null
        if deadline.isBefore(today)
          stdout.println("Overdue")

Correct:

      if deadline <- findDeadline()
        stdout.println(`Deadline: ${deadline}`)
      else
Other ways to ask this
  • Use a guard to safely handle an optional deadline and compare it with today.
  • I need to check whether a deadline exists and if so whether it has passed.
  • In Java I'd check if deadline != null && deadline.isBefore(today). How does EK9 do this?
  • Guard a date lookup and then compare the result against the current date.

Coming from another language?

Java: Date d = findDeadline(); if (d != null && d.isBefore(today)). Python: if (d := find_deadline()) and d < today. Rust: if let Some(d) = find_deadline() { if d < today }. Go: if d := findDeadline(); d != nil && d.Before(today). EK9: if deadline <- findDeadline() with deadline < today — one expression for declare + isSet + compare.

Keywords: comparison, today, deadline, guard, check, control flow, with, date, future, past