# Extending Base.in type-stably

**URL:** <https://discourse.julialang.org/t/extending-base-in-type-stably/5341>\
**Category:** General Usage\
**Created:** [August 11, 2017, 11:11pm UTC](https://discourse.julialang.org/t/extending-base-in-type-stably/5341 "2017-08-11T23:11:23Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [August 12, 2017, 8:46pm UTC](https://discourse.julialang.org/t/extending-base-in-type-stably/5341/12 "2017-08-12T20:46:36Z")

</div>

So I tried to remove return type instability of all `in` methods of `Base` module, but the problem still persists.

Specifically, I tracked down all the `in` methods returning `Any` in the `Base` module, and annotated their return types wth `::Bool`. Then I could see that `Base.in` always returns either `Union{}` or `Bool`:

```julia
julia> VERSION
v"0.7.0-DEV.1283"

julia> Base.return_types(Base.in)
28-element Array{Any,1}:
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Union{}
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Bool
 Union{}
 Bool
 Bool
 Bool
 Bool

```

Then, I repeated the original experiment with `MyAbs` and `MyType`s, hoping that now type stability of the extended `Base.in` would be recovered. Unfortunately it didn’t:

```julia
julia> @code_warntype zero_in(mvec)
Variables:
  #self#::#zero_in
  mvec::Array{MyAbs,1}
  t::Any
  #temp#::Int64
  m::MyAbs

Body:
  begin
      t::Any = false
      #= line 4 =#
      #temp#::Int64 = 1
      4:
      unless (Base.not_int)((#temp#::Int64 === (Base.add_int)((Base.arraylen)(mvec::Array{MyAbs,1})::Int64, 1)::Int64)::Bool)::Bool goto 17
      SSAValue(2) = $(Expr(:invoke, MethodInstance for getindex(::Array{MyAbs,1}, ::Int64), :(Base.getindex), :(mvec), :(#temp#)))::MyAbs
      SSAValue(3) = (Base.add_int)(#temp#::Int64, 1)::Int64
      m::MyAbs = SSAValue(2)
      #temp#::Int64 = SSAValue(3)
      #= line 5 =#
      t::Any = (0 in m::MyAbs)::Any
      #= line 6 =#
      unless t::Any goto 15
      goto 17
      15:
      goto 4
      17:
      #= line 8 =#
      return t::Any
  end::Any

```

So I guess our analysis was wrong. There is something else causing this instability.

---

_[View the full topic](https://discourse.julialang.org/t/extending-base-in-type-stably/5341)._
