thautwarm / MLStyle.jl

Julia functional programming infrastructures and metaprogramming facilities
https://thautwarm.github.io/MLStyle.jl/latest/
MIT License
404 stars 39 forks source link

Where clauses for value predicates #128

Closed jtrakk closed 3 years ago

jtrakk commented 3 years ago

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)"))
thautwarm commented 3 years ago

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
jtrakk commented 3 years ago

I guess that's the same problem as https://github.com/JuliaLang/julia/issues/21847

jariji commented 6 months ago

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.