# Doing strange things with DataTypes in functions

**URL:** <https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 17, 2021, 1:35pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086 "2021-06-17T13:35:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 1:35pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086/1 "2021-06-17T13:35:53Z")

</div>

Hey all 🙂

Initially I wanted to do this:

```julia
julia> f{T}(i::T) = i
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] top-level scope
   @ REPL[51]:1

```

But it only works for constructor of structs.

Then I thought I could do that:

```julia
julia> f(T::DataType, i::T) = i
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] top-level scope
   @ REPL[50]:1

```

But that also doesn’t work.

(I know that I don’t need to specify the datatype but I want for better readability)

Does anyone know how to do that?

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 1:40pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086/2 "2021-06-17T13:40:51Z")

</div>

That’s what I want 😃

```julia
f(::Type{T}, i::T) where T = i

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 17, 2021, 1:42pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086/3 "2021-06-17T13:42:26Z")

</div>

Why not simply `f(i::T) where T = i` ?

(Assuming you want to use the value `T` in your function body…)

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 1:46pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086/4 "2021-06-17T13:46:50Z")

</div>

That’s the actual structure. (Var names changed…)

```julia
function some_name(
    ::Type{T},
    counter::Int, 
    initial_bars::Vector{Dict{OtherType,T}},
    dict_name::Dict{OtherType, MyType},
)::Dict{OtherType,T} where T

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [June 17, 2021, 2:07pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086/5 "2021-06-17T14:07:23Z")

</div>

The first argument seems redundant, I think you get the same functionality with the following?

```julia
function some_name(
    counter::Int, 
    initial_bars::Vector{Dict{OtherType,T}},
    dict_name::Dict{OtherType, MyType},
)::Dict{OtherType,T} where T

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [June 17, 2021, 2:21pm UTC](https://discourse.julialang.org/t/doing-strange-things-with-datatypes-in-functions/63086/6 "2021-06-17T14:21:15Z")

</div>

That’s true. But the `T` is a bit hidden. The first arg is only for readability 🙂
