# How to make a function defined from getindex type safe?

**URL:** <https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 24, 2019, 6:54am UTC](https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647 "2019-12-24T06:54:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [December 24, 2019, 6:54am UTC](https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647/1 "2019-12-24T06:54:09Z")

</div>

```julia
testv = [1,2,3,4,5]
testf(n) = testv[n+1]
@code_warntype testf(4)

```

This gives me

```julia
Variables
  #self#::Core.Compiler.Const(testf, false)
  n::Int64

Body::Any
1 ─ %1 = (n + 1)::Int64
│ %2 = Base.getindex(Main.testv, %1)::Any
└── return %2

```

How can I define such a function in a type-safe way?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 24, 2019, 7:46am UTC](https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647/2 "2019-12-24T07:46:02Z")

</div>

You are accessing a global variable whose type may change. Pass testv as an argument to the function instead.

---

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [December 24, 2019, 12:15pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647/3 "2019-12-24T12:15:40Z")

</div>

Thanks a lot! I figured it out later on the goal can also be achieved by making testv `const`.

Here I get another one:

```julia
test(x) = x>0 ? 1.0 : 1im

```

How to make this one type safe without annotating it as Complex64? That is, I want Float64 for positive argument and Complex{Float64} otherwise.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 24, 2019, 12:53pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647/4 "2019-12-24T12:53:34Z")

</div>

That’s just not going to be type stable. The closest you could do is return `1.0+0im`

---

<div class="post-metadata">

**Author:** ![zwwi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zwwi/32/11932_2.png) [@zwwi](https://discourse.julialang.org/u/zwwi)\
**Post date:** [December 24, 2019, 1:17pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-defined-from-getindex-type-safe/32647/5 "2019-12-24T13:17:29Z")

</div>

Thanks.
