# Using type parameters (with where) in functions

**URL:** <https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156>\
**Category:** New to Julia\
**Created:** [November 18, 2017, 7:46pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156 "2017-11-18T19:46:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 18, 2017, 7:46pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156/1 "2017-11-18T19:46:16Z")

</div>

I’ve tried the following:

```julia
f{T}() where T = typeof(T)
f{Int64}()

```

with no luck, the first line returning `UndefVarError: f not defined`. As I understood type parameters, things like this should be possible, since the type is inferred from the method signature (`where`), but this seems to work only for types, not functions or methods, is this correct?

---

<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:** [November 18, 2017, 8:14pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156/2 "2017-11-18T20:14:59Z")

</div>

> [@wrgr](#):
>
> f{T}() where T = typeof(T)

It’s not clear what you are trying to do. How about

```julia
julia> f(x::T) where {T} = T
f (generic function with 1 method)

julia> f(3)
Int64

```

---

<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:** [November 18, 2017, 8:35pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156/3 "2017-11-18T20:35:41Z")

</div>

> [@wrgr](#):
>
> f{Int64}()

No that’s not supported since type parameter is a property of the method and not the function. You can either do `f(Int64)` (and write function to just take a type as argument) or explicitly make `f` a type i.e. `struct f{T} end` and define call on that.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 18, 2017, 11:05pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156/4 "2017-11-18T23:05:08Z")

</div>

> [@dpsanders](#):
>
> It’s not clear what you are trying to do

It’s a simplistic example of a generic function (method), which doesn’t have mentioning of T in its parameters signature.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 18, 2017, 11:09pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156/5 "2017-11-18T23:09:10Z")

</div>

> [@yuyichao](#):
>
> explicitly make f a type i.e. struct f{T} end and define call on that

Does this have any overhead worth mentioning, or in the end is no different than using standard methods?

---

<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:** [November 18, 2017, 11:30pm UTC](https://discourse.julialang.org/t/using-type-parameters-with-where-in-functions/7156/6 "2017-11-18T23:30:17Z")

</div>

It is not recommended since it offer no advantage over passing the type as a normal parameter. Using special type for this should not add runtime overhead when things are properly inferred. There are different kind of overhead when things aren’t inferrend in either cases.
