# Use of \_default\_eltype

**URL:** <https://discourse.julialang.org/t/use-of--default-eltype/1624>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 20, 2017, 11:27pm UTC](https://discourse.julialang.org/t/use-of--default-eltype/1624 "2017-01-20T23:27:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FANG\_Colin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fang_colin/32/170_2.png) [@FANG\_Colin](https://discourse.julialang.org/u/FANG_Colin)\
**Post date:** [January 20, 2017, 11:27pm UTC](https://discourse.julialang.org/t/use-of--default-eltype/1624/1 "2017-01-20T23:27:12Z")

</div>

I have seen code in `set`, `collect` make use of `_default_eltype(typeof(g))` which basically calls `Core.Inference....`

I don’t know Julia internal very well. My understanding is that `Core.Inference` is solely for compilation. It feels a bit odd to do type inference in run time. Wouldn’t it be slow?

E.g. `collect(g::Generator)` would have to invoke run time inference for each `g`

Do Julia libs normally call `Inference` in run time if static inferences are insufficient?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 20, 2017, 11:35pm UTC](https://discourse.julialang.org/t/use-of--default-eltype/1624/2 "2017-01-20T23:35:40Z")

</div>

It’s a hack so that comprehensions/generators can be mostly type stable while stop people from complaining that they don’t work for empty input. It’s only used in cases where the type cannot be decided from runtime values (empty iterator) and is not computable (which is the case in general).

type inference has special handling to infer the result of this function statically. If this can’t be done then yes, there will be a slow inference call at runtime. It’ll only be used for empty case so it shouldn’t matter that much.

---

<div class="post-metadata">

**Author:** ![FANG\_Colin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fang_colin/32/170_2.png) [@FANG\_Colin](https://discourse.julialang.org/u/FANG_Colin)\
**Post date:** [January 21, 2017, 2:30pm UTC](https://discourse.julialang.org/t/use-of--default-eltype/1624/3 "2017-01-21T14:30:44Z")

</div>

```julia
function Set(g::Generator)
    T = _default_eltype(typeof(g))
    (isleaftype(T) || T === Union{}) || return grow_to!(Set{T}(), g)
    return Set{T}(g)
end

function collect(itr::Generator)
    isz = iteratorsize(itr.iter)
    et = _default_eltype(typeof(itr))
    if isa(isz, SizeUnknown)
        return grow_to!(Array{et,1}(0), itr)
    else
        st = start(itr)
        if done(itr,st)
            return _array_for(et, itr.iter, isz)
        end
        v1, st = next(itr, st)
        collect_to_with_first!(_array_for(typeof(v1), itr.iter, isz), v1, itr, st)
    end
end

```

In these examples, `_default_eltype` is always called regardless of generator being empty or not.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [January 23, 2017, 5:11pm UTC](https://discourse.julialang.org/t/use-of--default-eltype/1624/4 "2017-01-23T17:11:13Z")

</div>

> [@FANG\_Colin](#):
>
> In these examples, \_default\_eltype is always called regardless of generator being empty or not.

It’s called, but when possible inference will compute it at compile time, so that there’s no runtime overhead for type stable cases. For other cases, maybe the call could be moved below the conditionals so that it’s effectively not called when not used. Maybe the compiler already performs this optimization though – one would have to check.
