# Using \`\<:\`(subtype evaluate-operator) in the Conditional Evaluation (if-else)

**URL:** https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309
**Category:** General Usage
**Tags:** question
**Created:** [January 31, 2021, 3:38am UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309 "2021-01-31T03:38:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [January 31, 2021, 3:38am UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309/1 "2021-01-31T03:38:33Z")

</div>

MWE:

```julia
julia> a = [1,2]
2-element Array{Int64,1}:
 1
 2

julia> typeof(a) <: Array{<:AbstractArray, 1}
false

julia> if typeof(a) <: Array{<:AbstractArray, 1} == false
       println("False!")
       end

julia> if typeof(a) <: Array{<:AbstractArray, 1}
       println("False!")
       end

julia> if (typeof(a) <: Array{<:AbstractArray, 1})
       println("False!")
       end

julia> if (typeof(a) <: Array{<:AbstractArray, 1}) == false
       println("False!")
       end
False!

```

Is this an intentional design? Why do I have to put a pair of parentheses outside `typeof(a) <: Array{<:AbstractArray, 1}` instead of just using `<:` like other binary operators such as `<`?  
Thank you!

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 31, 2021, 8:58am UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309/2 "2021-01-31T08:58:44Z")

</div>

Ok, this took me a bit to figure out.

**TL;DR:** chaining comparisons use short-circuiting thus the comparison in OP does not reach `== false`. More readable is to use `!`:

```julia
!(typeof(a) <: Array{<:AbstractArray, 1})

```

**Long story:**

In hind-sight it shouldn’t have taken so long to figure out. Here how I went about it:

Further reduced:

```julia
julia> Int <: Float64 == false
false

julia> (Int <: Float64) == false
true

julia> Int <: (Float64 == false)
ERROR: TypeError: in <:, expected Type, got a value of type Bool
Stacktrace:
 [1] top-level scope at REPL[15]:1

```

Looking at the lowered code for the top example:

```julia
julia> Meta.lower(Main, :(Int <: Float64 == false))
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Int <: Float64
└── goto #3 if not %1
2 ─ %3 = Float64 == false
└── return %3
3 ─ return false
))))

```

which can be tidied up to give:

```julia
if Int <: Float64
    false
else
    Float64 == false
end

```

And that finally made it click for me: chaining comparison use short-circuiting (see [Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Chaining-comparisons)). Thus

```julia
Int <: Float64 == false # -> false

```

is not equivalent to

```julia
(Int <: Float64) == false

```

but to

```julia
Int <: Float64 && Float64 == false # (edited)

```

and as the first term is false, the second never gets evaluated.

Or put into another example:

```nohighlight
julia> 5 < 4 > "haha"
false

julia> 2 < 4 > "haha"
ERROR: MethodError: no method matching isless(::String, ::Int64)

```

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [January 31, 2021, 10:04am UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309/3 "2021-01-31T10:04:19Z")

</div>

> [@mauro3](#):
>
> but to
> 
> ```julia
> Int <: Float64 && (Int <: Float64) == false
> 
> ```

Shouldn’t it be:…?

```julia
Int <: Float64 && Float64 == false

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 31, 2021, 10:41am UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309/4 "2021-01-31T10:41:30Z")

</div>

Yes, thanks. Edited above.

---

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [February 1, 2021, 8:52pm UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309/5 "2021-02-01T20:52:48Z")

</div>

> [@mauro3](#):
>
> `Meta.lower(Main, :(Int <: Float64 == false))`

Thanks! this helps clarify a lot!

---

<div class="post-metadata">

### Author: ![cojua8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cojua8/32/11882_2.png) [@cojua8](https://discourse.julialang.org/u/cojua8)
#### Post date: [February 2, 2021, 7:07pm UTC](https://discourse.julialang.org/t/using-subtype-evaluate-operator-in-the-conditional-evaluation-if-else/54309/6 "2021-02-02T19:07:54Z")

</div>

For type checking, consider using the `isa` operator:

```julia
if !(a isa Array{<:AbstractArray, 1})
    println("False!")
end

```
