From adb21c7aef112c291b3440e45fd0db6bb9ac3502 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Wed, 21 Dec 2016 19:31:23 -0800 Subject: [PATCH 1/2] Add `isa` as an infix operator --- base/deprecated.jl | 2 +- src/julia-parser.scm | 7 ++++--- test/operators.jl | 3 +++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index d16ac4a791185..31b5fa36b0ce6 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -265,7 +265,7 @@ export fieldoffsets # 14766 @deprecate write(io::IO, p::Ptr, nb::Integer) unsafe_write(io, p, nb) -@deprecate isgeneric(f) isa(f,Function) +@deprecate(isgeneric(f), isa(f, Function)) # need to do this manually since the front end deprecates method defs of `call` const call = @eval function(f, args...; kw...) diff --git a/src/julia-parser.scm b/src/julia-parser.scm index 44692bb8dff6f..dc6cd88a1196a 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -17,7 +17,7 @@ (define prec-lazy-and '(&&)) (define prec-comparison (append! '(|<:| |>:| |.!|) - (add-dots '(> < >= ≥ <= ≤ == === ≡ != ≠ !== ≢ ∈ ∉ ∋ ∌ ⊆ ⊈ ⊂ ⊄ ⊊ ∝ ∊ ∍ ∥ ∦ ∷ ∺ ∻ ∽ ∾ ≁ ≃ ≄ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ≍ ≎ ≐ ≑ ≒ ≓ ≔ ≕ ≖ ≗ ≘ ≙ ≚ ≛ ≜ ≝ ≞ ≟ ≣ ≦ ≧ ≨ ≩ ≪ ≫ ≬ ≭ ≮ ≯ ≰ ≱ ≲ ≳ ≴ ≵ ≶ ≷ ≸ ≹ ≺ ≻ ≼ ≽ ≾ ≿ ⊀ ⊁ ⊃ ⊅ ⊇ ⊉ ⊋ ⊏ ⊐ ⊑ ⊒ ⊜ ⊩ ⊬ ⊮ ⊰ ⊱ ⊲ ⊳ ⊴ ⊵ ⊶ ⊷ ⋍ ⋐ ⋑ ⋕ ⋖ ⋗ ⋘ ⋙ ⋚ ⋛ ⋜ ⋝ ⋞ ⋟ ⋠ ⋡ ⋢ ⋣ ⋤ ⋥ ⋦ ⋧ ⋨ ⋩ ⋪ ⋫ ⋬ ⋭ ⋲ ⋳ ⋴ ⋵ ⋶ ⋷ ⋸ ⋹ ⋺ ⋻ ⋼ ⋽ ⋾ ⋿ ⟈ ⟉ ⟒ ⦷ ⧀ ⧁ ⧡ ⧣ ⧤ ⧥ ⩦ ⩧ ⩪ ⩫ ⩬ ⩭ ⩮ ⩯ ⩰ ⩱ ⩲ ⩳ ⩴ ⩵ ⩶ ⩷ ⩸ ⩹ ⩺ ⩻ ⩼ ⩽ ⩾ ⩿ ⪀ ⪁ ⪂ ⪃ ⪄ ⪅ ⪆ ⪇ ⪈ ⪉ ⪊ ⪋ ⪌ ⪍ ⪎ ⪏ ⪐ ⪑ ⪒ ⪓ ⪔ ⪕ ⪖ ⪗ ⪘ ⪙ ⪚ ⪛ ⪜ ⪝ ⪞ ⪟ ⪠ ⪡ ⪢ ⪣ ⪤ ⪥ ⪦ ⪧ ⪨ ⪩ ⪪ ⪫ ⪬ ⪭ ⪮ ⪯ ⪰ ⪱ ⪲ ⪳ ⪴ ⪵ ⪶ ⪷ ⪸ ⪹ ⪺ ⪻ ⪼ ⪽ ⪾ ⪿ ⫀ ⫁ ⫂ ⫃ ⫄ ⫅ ⫆ ⫇ ⫈ ⫉ ⫊ ⫋ ⫌ ⫍ ⫎ ⫏ ⫐ ⫑ ⫒ ⫓ ⫔ ⫕ ⫖ ⫗ ⫘ ⫙ ⫷ ⫸ ⫹ ⫺ ⊢ ⊣)))) ;; (plus `in`) + (add-dots '(> < >= ≥ <= ≤ == === ≡ != ≠ !== ≢ ∈ ∉ ∋ ∌ ⊆ ⊈ ⊂ ⊄ ⊊ ∝ ∊ ∍ ∥ ∦ ∷ ∺ ∻ ∽ ∾ ≁ ≃ ≄ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ≍ ≎ ≐ ≑ ≒ ≓ ≔ ≕ ≖ ≗ ≘ ≙ ≚ ≛ ≜ ≝ ≞ ≟ ≣ ≦ ≧ ≨ ≩ ≪ ≫ ≬ ≭ ≮ ≯ ≰ ≱ ≲ ≳ ≴ ≵ ≶ ≷ ≸ ≹ ≺ ≻ ≼ ≽ ≾ ≿ ⊀ ⊁ ⊃ ⊅ ⊇ ⊉ ⊋ ⊏ ⊐ ⊑ ⊒ ⊜ ⊩ ⊬ ⊮ ⊰ ⊱ ⊲ ⊳ ⊴ ⊵ ⊶ ⊷ ⋍ ⋐ ⋑ ⋕ ⋖ ⋗ ⋘ ⋙ ⋚ ⋛ ⋜ ⋝ ⋞ ⋟ ⋠ ⋡ ⋢ ⋣ ⋤ ⋥ ⋦ ⋧ ⋨ ⋩ ⋪ ⋫ ⋬ ⋭ ⋲ ⋳ ⋴ ⋵ ⋶ ⋷ ⋸ ⋹ ⋺ ⋻ ⋼ ⋽ ⋾ ⋿ ⟈ ⟉ ⟒ ⦷ ⧀ ⧁ ⧡ ⧣ ⧤ ⧥ ⩦ ⩧ ⩪ ⩫ ⩬ ⩭ ⩮ ⩯ ⩰ ⩱ ⩲ ⩳ ⩴ ⩵ ⩶ ⩷ ⩸ ⩹ ⩺ ⩻ ⩼ ⩽ ⩾ ⩿ ⪀ ⪁ ⪂ ⪃ ⪄ ⪅ ⪆ ⪇ ⪈ ⪉ ⪊ ⪋ ⪌ ⪍ ⪎ ⪏ ⪐ ⪑ ⪒ ⪓ ⪔ ⪕ ⪖ ⪗ ⪘ ⪙ ⪚ ⪛ ⪜ ⪝ ⪞ ⪟ ⪠ ⪡ ⪢ ⪣ ⪤ ⪥ ⪦ ⪧ ⪨ ⪩ ⪪ ⪫ ⪬ ⪭ ⪮ ⪯ ⪰ ⪱ ⪲ ⪳ ⪴ ⪵ ⪶ ⪷ ⪸ ⪹ ⪺ ⪻ ⪼ ⪽ ⪾ ⪿ ⫀ ⫁ ⫂ ⫃ ⫄ ⫅ ⫆ ⫇ ⫈ ⫉ ⫊ ⫋ ⫌ ⫍ ⫎ ⫏ ⫐ ⫑ ⫒ ⫓ ⫔ ⫕ ⫖ ⫗ ⫘ ⫙ ⫷ ⫸ ⫹ ⫺ ⊢ ⊣)))) ;; (plus `in` and `isa`) (define prec-pipe (add-dots '(|\|>| |<\||))) (define prec-colon '(: |..|)) (define prec-plus (append! '($) @@ -56,7 +56,7 @@ (define is-prec-comparison? (let ((compare-ops (Set prec-comparison))) (lambda (t) - (or (compare-ops t) (eq? t 'in))))) + (or (compare-ops t) (or (eq? t 'in) (eq? t 'isa)))))) ;; hash table of binary operators -> precedence (define prec-table (let ((t (table))) @@ -67,7 +67,8 @@ (pushprec (cdr L) (+ prec 1))))) (pushprec (map eval prec-names) 1) t)) -(put! prec-table 'in (get prec-table '== 0)) ; add `in` to the prec-table +(put! prec-table 'in (get prec-table '== 0)) ; add `in` to the prec-table +(put! prec-table 'isa (get prec-table '== 0)) ; add `isa` to the prec-table (define (operator-precedence op) (get prec-table op 0)) (define unary-ops '(+ - ! ¬ ~ |<:| |>:| √ ∛ ∜)) diff --git a/test/operators.jl b/test/operators.jl index 622afc9fa14df..36c48dfe4d68f 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -34,6 +34,9 @@ B = [true true false] @test isa([:a=>1, :b=>2.0], Vector{Pair{Symbol,Float64}}) @test isa(["a"=>1, :b=>2.0], Vector{Pair{Any,Float64}}) +# Infix `isa` +@test 1 isa Integer + p = 1=>:foo @test first(p) == 1 @test last(p) == :foo From cbee357f2afd1514196cacdd451c3b0f1bb1730f Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Wed, 21 Dec 2016 20:20:01 -0800 Subject: [PATCH 2/2] Document availability of infix isa --- base/docs/helpdb/Base.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index cb199b0d0c6c8..6c56b036b0962 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -2457,7 +2457,8 @@ readavailable """ isa(x, type) -> Bool -Determine whether `x` is of the given `type`. +Determine whether `x` is of the given `type`. Can also be used as an infix operator, e.g. +`x isa type`. """ isa