thautwarm/MLStyle.jl

Where clauses for value predicates

Closed this issue · 3 comments

Currently we can use where for types, as in advanced type patterns. We also have if/end and GuardBy.

Can we also use where for value predicates like OCaml's when clause?

julia> @match 1 begin
       x where x < 2 => 10
       _ => 20
       end
ERROR: LoadError: PatternCompilationError(:(#= REPL[13]:2 =#), ErrorException("unknown pattern syntax :(x where x < 2)"))

In the early days we use where, however the precedences of expressions get wrong.
For instance, if you use a range pattern 1:2, but you get a 1: (2 where a).

julia> dump(:(1:2 where a ))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol :
    2: Int64 1
    3: Expr
      head: Symbol where
      args: Array{Any}((2,))
        1: Int64 2
        2: Symbol a

I guess that's the same problem as JuliaLang/julia#21847

Is a:b where p the main case we're worried about? x where x < 2 => 2 seems like a pretty common case and afaict doesn't have that problem.