# Type stability when DataType is passed as a keyword argument

**URL:** <https://discourse.julialang.org/t/type-stability-when-datatype-is-passed-as-a-keyword-argument/52770>\
**Category:** General Usage\
**Tags:** question, type-stability, functions\
**Created:** [January 3, 2021, 10:09am UTC](https://discourse.julialang.org/t/type-stability-when-datatype-is-passed-as-a-keyword-argument/52770 "2021-01-03T10:09:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 3, 2021, 10:09am UTC](https://discourse.julialang.org/t/type-stability-when-datatype-is-passed-as-a-keyword-argument/52770/1 "2021-01-03T10:09:53Z")

</div>

Hi all-

I have a question about type stability when a `DataType` is passed as a keyword argument. The code below shows all combinations of positional vs keyword arguments and `Float64` vs `DataType`. Why is the `DataType` keyword combination not type stable while the others are type stable? This holds true even with `f(;T::DataType) = f(T)`.

Thank you

```julia
f(T::DataType) = Vector{T}(undef,1)

f(T::Float64) = T

f(;T) = f(T)

```

Here is a summary of the results:

```julia
# type stable

@code_warntype f(Int)

# type stable

@code_warntype f(.5)

# type stable

@code_warntype f(T=.5)

# not type stable

@code_warntype f(T=Int)

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 3, 2021, 1:52pm UTC](https://discourse.julialang.org/t/type-stability-when-datatype-is-passed-as-a-keyword-argument/52770/2 "2021-01-03T13:52:38Z")

</div>

Keyword parameters do not participate in dispatch and functions do not specialize on them. The type of the returned value depend on `T`, therefore if the function takes `T` as a keyword argument without dispatching/specializing on it, it is not able to infer the type of the returned value without running the function, and this leads to type unstability. Also, there is no guarantee that your `f` with positional arguments will stay type-stable, I think inference may give up if you add 3 more methods for different types.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 3, 2021, 2:29pm UTC](https://discourse.julialang.org/t/type-stability-when-datatype-is-passed-as-a-keyword-argument/52770/3 "2021-01-03T14:29:34Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [January 3, 2021, 8:57pm UTC](https://discourse.julialang.org/t/type-stability-when-datatype-is-passed-as-a-keyword-argument/52770/4 "2021-01-03T20:57:56Z")

</div>

Note however that with “constant propagation” the compiler can still figure that last case out, its just that _all_ of the code needs to be inside a function, e.g. this works:

```julia
@code_warntype (()->f(T=Int))()

```
