Julia Match.jl match error with Date - pattern expects 3 fields

Just updated my project deps and am getting this really strange error with a Match.jl @match statement.

ERROR: The type `Dates.Date` has 1 fields but the pattern expects 3 fields.
    
@match date begin
    Date(2023, 4, 10) => true # Monday after Easter
    Date(2023, 5, 1) => true # May Day?
end

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] bind_pattern!(location::LineNumberNode, source::Expr, input::Symbol, binder::Match.BinderContext, assigned::Base.ImmutableDict{Symbol, Symbol})
    @ Match ~/.julia/packages/Match/SZDEk/src/binding.jl:244
  [3] bind_case(case_number::Int64, location::LineNumberNode, case::Expr, predeclared_temps::Vector{Any}, binder::Match.BinderContext)
    @ Match ~/.julia/packages/Match/SZDEk/src/binding.jl:580
  [4] build_automaton_core(value::Symbol, source_cases::Vector{Any}, location::LineNumberNode, predeclared_temps::Vector{Any}, binder::Match.BinderContext)
    @ Match ~/.julia/packages/Match/SZDEk/src/match_cases_opt.jl:15
  [5] build_automaton(location::LineNumberNode, mod::Module, value::Any, body::Expr)
    @ Match ~/.julia/packages/Match/SZDEk/src/match_cases_opt.jl:149
  [6] build_deduplicated_automaton
    @ ~/.julia/packages/Match/SZDEk/src/match_cases_opt.jl:158 [inlined]
  [7] handle_match_cases(location::LineNumberNode, mod::Module, value::Symbol, body::Expr)
    @ Match ~/.julia/packages/Match/SZDEk/src/match_cases_opt.jl:477
  [8] var"@match"(__source__::LineNumberNode, __module__::Module, value::Any, cases::Any)
    @ Match ~/.julia/packages/Match/SZDEk/src/matchmacro.jl:114
  [9] include(mod::Module, _path::String)
    @ Base ./Base.jl:457
 [10] include(x::String)
    @ Main.Tibra ~/src/tibra/Tibra.jl/src/Tibra.jl:1
 [11] top-level scope
    @ ~/src/tibra/Tibra.jl/src/Tibra.jl:101
 [12] include(fname::String)
    @ Base.MainInclude ./client.jl:478
 [13] top-level scope
    @ ~/src/tibra/Tibra.jl/scripts/run_tibrax_server.jl:1

Julia’s declaration for this type has not changed for some time:

struct Date <: TimeType
    instant::UTInstant{Day}
    Date(instant::UTInstant{Day}) = new(instant)
end

I recently updated the Match.jl package to catch the kind of error you made - attempting to match the wrong number of fields. That code would never have matched before. Now you get a compile-time error.

You can also use interpolation syntax for this case, since you are looking for a specific date:

@match date begin
    $(Date(2023, 4, 10)) => true # Monday after Easter
    $(Date(2023, 5, 1)) => true # May Day?
end
1 Like

@thautwarm 's MLStyle.jl — MLStyle.jl Documentation can do composable pattern matching, for example:

using Dates, MLStyle

@active Date(date) begin
    if date isa Dates.Date
        (year(date), month(date), dayofmonth(date))
    else
        nothing
    end
end

# Get third day of month
@match Date(2000,12,31) begin
    Date(y,m,_) => Date(y,m,3)
end
# 2000-12-03