# Initialize an array with the type of a function return

**URL:** <https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 20, 2018, 9:45am UTC](https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755 "2018-08-20T09:45:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [August 20, 2018, 9:45am UTC](https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755/1 "2018-08-20T09:45:42Z")

</div>

Hello, I have the following setup: I want to initialize an array with a element-type which is given by a return type of a function. Something like this:

```julia
A = Array{typeof(f(1))}(undef, 10)
for 1 in 1:10 
   A[i] = f(i) 
end 

```

This works but it calls the function `f(1)` explicitly. is there a good way to just get the return type without the need to call the function. In the real case the call is a time-heavy calculation.

I was able to use `Base.return_types(f, (Int,))[1]` but this seams to be a clumsy way, isn’t it?

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [August 20, 2018, 9:51am UTC](https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755/2 "2018-08-20T09:51:13Z")

</div>

There is some discussion here [https://docs.julialang.org/en/stable/manual/methods/#Output-type-computation-1](https://docs.julialang.org/en/stable/manual/methods/#Output-type-computation-1)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 20, 2018, 10:06am UTC](https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755/3 "2018-08-20T10:06:14Z")

</div>

For this specific example you would do `[f(i) for i in 1:10]`. Do you have another concrete example why you need to do this?

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [August 20, 2018, 10:15am UTC](https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755/4 "2018-08-20T10:15:03Z")

</div>

After asking this question I realized this possibility as well. No, for now I do not have a good idea why one could **not** use this array-syntax.

@Evizero: Thanks for linking to the article. It is good to know the general possibilities.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 20, 2018, 6:18pm UTC](https://discourse.julialang.org/t/initialize-an-array-with-the-type-of-a-function-return/13755/5 "2018-08-20T18:18:44Z")

</div>

There’s some more discussion here: [Ridiculous idea: types from the future](https://discourse.julialang.org/t/ridiculous-idea-types-from-the-future/5457) in case you’re interested.
