# If I have a string such as, "Float64", can I convert it to the Julia DataType?

**URL:** <https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035>\
**Category:** General Usage\
**Created:** [February 10, 2024, 5:22pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035 "2024-02-10T17:22:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 10, 2024, 5:22pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/1 "2024-02-10T17:22:21Z")

</div>

Basically, if I have my type written in a string format, can I convert it to the actual DataType inside of Julia?

Kind regards

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 10, 2024, 5:39pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/2 "2024-02-10T17:39:14Z")

</div>

> [@Ahmed\_Salih](#):
>
> Basically, if I have my type written in a string format, can I convert it to the actual DataType inside of Julia?

```julia
julia> eval(Meta.parse("Float64"))
Float64

```

(But if at all possible, you should avoid passing information like this as strings. There are plenty of other ways to serialize data.)

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [February 10, 2024, 6:03pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/3 "2024-02-10T18:03:02Z")

</div>

In simple cases, you can do something like

```julia-repl
julia> getproperty(Base, Meta.parse("Float64"))
Float64

```

which I think is (slightly) better than using `eval`. Note that this trick does not easily generalize to more complex situations (like e.g. `"Vector{Int}"`).

That being said, I’d still stick to the advice above and avoid doing such things if possible.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 10, 2024, 6:04pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/4 "2024-02-10T18:04:20Z")

</div>

> [@ffevotte](#):
>
> In simple cases, you can do something like

If it’s just a single symbol, you don’t even need `Meta.parse` — you can do `getproperty(Base, Symbol("Float64"))` … but needing to do this is often a sign that you should re-think what you are doing.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 10, 2024, 6:34pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/5 "2024-02-10T18:34:28Z")

</div>

> [@ffevotte](#):
>
> `getproperty(Base, Meta.parse("Float64"))`

Thank you @ffevotte I really like this solution! Exactly what I need.

@stevengj I fully want to avoid `eval` it is a bit spooky to me. In my case I have a data format with a dict and some low level data types, i.e. Float64, Int32 etc. So ffevotte solution avoids eval and is perfect in this case.

Still I am quite curious, do you have an example of “needing to do this…” ? I would like to understand better 🙂

EDIT: Unsure who to mark as solution 🙂  
EDIT2: Marked steven’s answer as solution, Meta.parse had a small bug when used inside of a for loop of a function extracting a string value from a dict, it would return Float64, but the sizeof it only as 7, so just a symbol not the type.

Kind regards

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 10, 2024, 6:45pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/6 "2024-02-10T18:45:20Z")

</div>

> [@Ahmed\_Salih](#):
>
> In my case I have a data format with a dict and some low level data types, i.e. Float64, Int32 etc.

A data format in a file? Why not use one of the existing serialization formats?

A data format in memory? Then you shouldn’t need to store anything as a string (except for strings).

> [@Ahmed\_Salih](#):
>
> do you have an example of “needing to do this…” ? I would like to understand better

It’s hard to give more information without some more context about your needs.

> [@Ahmed\_Salih](#):
>
> Unsure who to mark as solution

If you just have a symbol as a string, like `"Float32"`, it’s simplest and fastest to avoid both `parse` and `eval` as explained in [If I have a string such as, "Float64", can I convert it to the Julia DataType? - #4 by stevengj](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/4)

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 10, 2024, 6:52pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/7 "2024-02-10T18:52:06Z")

</div>

Got it, using `getproperty(Base, Symbol("Float64"))`.

And awesome, thanks for explaining. Will give it a go now and if starts to break, I will resort to making a dict for look up.

Kind regards

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 10, 2024, 7:16pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/8 "2024-02-10T19:16:55Z")

</div>

> [@Ahmed\_Salih](#):
>
> Meta.parse had a small bug when used inside of a for loop of a function extracting a string value from a dict, it would return Float64, but the sizeof it only as 7, so just a symbol not the type.

Not sure what you mean here. `Meta.parse` by itself will just return the symbol. You still have to call `getproperty` or similar to get the type.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 10, 2024, 10:26pm UTC](https://discourse.julialang.org/t/if-i-have-a-string-such-as-float64-can-i-convert-it-to-the-julia-datatype/110035/9 "2024-02-10T22:26:40Z")

</div>

If the strings to be converted refer to types, you could use such a scheme

```julia
function allsubtypes(T::Type)
    isconcretetype(T) && return T
    vcat(T , [allsubtypes(sT) for sT in subtypes(T)]...)
end
as=allsubtypes(Real)

d=Dict{String,Type}(zip(string.(as),as))

str="Float64"

vf=Vector{d[str]}()

```

```julia

function allsubtypes(T::Type)
    return isconcretetype(T) ? T : vcat(T , allsubtypes.(subtypes(T))...)
end

```
