MLStyle.jl icon indicating copy to clipboard operation
MLStyle.jl copied to clipboard

documentations for @active is out-of-date while error message is updated

Open thautwarm opened this issue 3 years ago • 0 comments

help?> @active
  Simple active pattern implementation. You can give a qualifier in the first argument of @active to customize its visibility in other modules.

      @active F(x) begin
          if x > 0
              nothing
          else
              :ok
          end
      end

      @match -1 begin
          F(:ok) => false
          _ => true
      end # true

      @active public IsEven(x) begin
          x % 2 === 0
      end

      @match 4 begin
          IsEven() => :ok
          _ => :err
      end # :ok

However,

struct ConstGRef end

MLStyle.@active ConstGRef(gref::GlobalRef) begin
    if isconst(gref.mod, gref.name)
        return getfield(gref.mod, gref.name)
    else
        nothing
    end
end


julia> @match GlobalRef(Base, :tuple) begin
           ConstGRef(1) => 1
       end
ERROR: invalid use of active patterns: 1-ary view pattern(ConstGRef(x)) should accept Union{Some{T}, Nothing} instead of Union{T, Nothing}! A simple solution is:
  (@active ConstGRef(x) ex) =>
  (@active ConstGRef(x) let r=ex; r === nothing? r : Some(r)) end

thautwarm avatar Jul 28 '22 07:07 thautwarm