# Array comprehension: Can someone explain this typeof result?

**URL:** https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597
**Category:** General Usage
**Tags:** question, type-stability
**Created:** [July 1, 2022, 5:17am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597 "2022-07-01T05:17:02Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [July 1, 2022, 5:17am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/1 "2022-07-01T05:17:02Z")

</div>

Running the code below, I expected Vector{Float64} for both cases. Is there an explanation for why  
I don’t get this for the `g` function?

```julia
function ff(y)
    function a(x)
        x>y ? x : ""
    end

    return a
end

g = ff(5)
f(x) = x>0 ? x : ""

println(typeof([Float64(f(x)) for x in zeros(0)]))
println(typeof([Float64(g(x)) for x in zeros(0)]))

```

The reason that this matters is that the sum function returns 0 for empty Float64 arrays, but throws an exception for Vector{Any}.

---

<div class="post-metadata">

### Author: ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)
#### Post date: [July 1, 2022, 5:36am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/2 "2022-07-01T05:36:05Z")

</div>

I don’t know why, but …

> [@GHTaarn](#):
>
> `Float64(f(x))`

```julia
julia> Float64("")
ERROR: MethodError: no method matching Float64(::String)
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at C:\Users\woclass\.julia\juliaup\julia-1.7.3+0~x64\share\julia\base\char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at C:\Users\woclass\.julia\juliaup\julia-1.7.3+0~x64\share\julia\base\twiceprecision.jl:255
  (::Type{T})(::Complex) where T<:Real at C:\Users\woclass\.julia\juliaup\julia-1.7.3+0~x64\share\julia\base\complex.jl:44
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[32]:1

julia> parse(Float64, "")
ERROR: ArgumentError: cannot parse "" as Float64
Stacktrace:
 [1] _parse_failure(T::Type, s::String, startpos::Int64, endpos::Int64) (repeats 2 times)
   @ Base .\parse.jl:373
 [2] #tryparse_internal#452
   @ .\parse.jl:369 [inlined]
 [3] tryparse_internal
   @ .\parse.jl:367 [inlined]
 [4] #parse#453
   @ .\parse.jl:379 [inlined]
 [5] parse(::Type{Float64}, s::String)
   @ Base .\parse.jl:379
 [6] top-level scope
   @ REPL[33]:1

```

Maybe `ArgumentError` is better than `MethodError`.

```julia
julia> [Float64(f(x)) for x in zeros(0)]
Float64[]

julia> [Float64(g(x)) for x in zeros(0)]
Any[]

julia> [parse(Float64,f(x)) for x in zeros(0)]
Float64[]

julia> [parse(Float64,g(x)) for x in zeros(0)]
Float64[]

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 1, 2022, 5:40am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/3 "2022-07-01T05:40:19Z")

</div>

`f` is implicitly `const`, and `g` is explicitly not `const`. The compiler doesn’t assume `g` won’t be reassigned in the middle of an array comprehension because it _could_ be by a side effect.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [July 1, 2022, 5:41am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/4 "2022-07-01T05:41:36Z")

</div>

> [@GHTaarn](#):
>
> I expected Vector{Float64} for both cases

In this case, the best strategy is to use `Float64[...]` in the comprehension.

```julia
julia> println(typeof(Float64[g(x) for x in zeros(0)]))
Vector{Float64}

```

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [July 1, 2022, 6:03am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/5 "2022-07-01T06:03:05Z")

</div>

Thank you, this explains it partially, but isn’t the result of the Float64 function always a Float64?

I tried a small modification and this actually works:

```julia
function ff(y)
    function a(x)
        x>y ? x : ""
    end

    return a
end

float64(x)::Float64 = Float64(x)

g = ff(5)
f(x) = x>0 ? x : ""

println(typeof([Float64(f(x)) for x in zeros(0)]))
println(typeof([float64(g(x)) for x in zeros(0)]))

```

So why would the compiler assume that float64 returns Float64, but not the Float64() function itself?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 1, 2022, 6:05am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/6 "2022-07-01T06:05:46Z")

</div>

> [@GHTaarn](#):
>
> isn’t the result of the Float64 function always a Float64?

It’s perfectly reasonable to expect so, but type constructors are just functions, and a method can return _anything_. See for yourself: `struct A end; Float64(::A) = 0; typeof(Float64(A()))`. This is rare, though. The more useful possibility is methods that throw errors, which are inferred with a “return type” of `Union{}`.

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [July 1, 2022, 6:14am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/7 "2022-07-01T06:14:14Z")

</div>

Okay, thank you. I will just assume that there is a good reason for this and use one of the solutions in this thread.

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [July 1, 2022, 6:23am UTC](https://discourse.julialang.org/t/array-comprehension-can-someone-explain-this-typeof-result/83597/8 "2022-07-01T06:23:02Z")

</div>

This was just a stripped down version of my own code to illustrate the problem. If you imagine that I might know that `x` always has positive values or that I am okay with having an error thrown if x is negative. But thanks for your reply.
