# Distinguish datatype from function

**URL:** <https://discourse.julialang.org/t/distinguish-datatype-from-function/126666>\
**Category:** New to Julia\
**Created:** [March 7, 2025, 8:40am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666 "2025-03-07T08:40:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [March 7, 2025, 8:40am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/1 "2025-03-07T08:40:32Z")

</div>

Hello!

Let’s say I want to define a dictionary from Strings to functions:

```julia
julia> temp = Dict{String, Function}
Dict{String, Function}

```

Then I add a value:

```julia
julia> temp["abc"] = DateTime
ERROR: MethodError: no method matching setindex!(::Type{Dict{String, Function}}, ::Type{DateTime}, ::String)

```

What’s up?

---

<div class="post-metadata">

**Author:** ![Soel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soel/32/214997_2.png) [@Soel](https://discourse.julialang.org/u/Soel)\
**Post date:** [March 7, 2025, 8:44am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/2 "2025-03-07T08:44:36Z")

</div>

maybe `temp=Dict{String,Function}()` ? (btw, DateTime is a type)

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [March 7, 2025, 8:46am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/3 "2025-03-07T08:46:51Z")

</div>

Yes, that’s better. But then I get:

```julia
julia> temp["abc"] = DateTime
ERROR: MethodError: Cannot `convert` an object of type Type{DateTime} to an object of type Function

```

---

<div class="post-metadata">

**Author:** ![Soel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soel/32/214997_2.png) [@Soel](https://discourse.julialang.org/u/Soel)\
**Post date:** [March 7, 2025, 8:47am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/4 "2025-03-07T08:47:26Z")

</div>

DateTime is a type… wanna refer to its constructor or what?

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [March 7, 2025, 8:47am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/5 "2025-03-07T08:47:41Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![Soel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soel/32/214997_2.png) [@Soel](https://discourse.julialang.org/u/Soel)\
**Post date:** [March 7, 2025, 8:49am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/6 "2025-03-07T08:49:29Z")

</div>

There’s a name conflict here, maybe creating a function `datetime(x) = DateTime(x)` and then perform your binding ?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 7, 2025, 8:53am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/7 "2025-03-07T08:53:11Z")

</div>

You can use

```julia
temp = Dict{String, Union{Function, Type}}()

```

If you want to be able to store both functions and types so you can call constructors. Depending on your use case it’s probably marginally more useful than just using a `Dict{String, Any}` though.

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [March 7, 2025, 8:54am UTC](https://discourse.julialang.org/t/distinguish-datatype-from-function/126666/9 "2025-03-07T08:54:19Z")

</div>

That works!
