# Converting String to DataType

**URL:** <https://discourse.julialang.org/t/converting-string-to-datatype/25504>\
**Category:** General Usage\
**Created:** [June 20, 2019, 11:19pm UTC](https://discourse.julialang.org/t/converting-string-to-datatype/25504 "2019-06-20T23:19:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dataSurfer](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@dataSurfer](https://discourse.julialang.org/u/dataSurfer)\
**Post date:** [June 20, 2019, 11:19pm UTC](https://discourse.julialang.org/t/converting-string-to-datatype/25504/1 "2019-06-20T23:19:25Z")

</div>

I’m using Julia v1.2 rc1 and I’d like to be able to convert a string to a data type but `eval(Symbol("SomeComplexType"))` isn’t doing it for types that are not basic. For instance this

```julia
eval(Symbol("Float64"))

```

returns `Float64` as is expected, but it won’t work for `eval(Symbol("Array{Float64}"))` since this is not a single symbol. It looks as if Julia used to be able to eval more complex types ([Parse String to DataType](https://discourse.julialang.org/t/parse-string-to-datatype/7118)) but that method doesn’t work anymore.

Would appreciate help on this.

Thanks!

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 20, 2019, 11:29pm UTC](https://discourse.julialang.org/t/converting-string-to-datatype/25504/2 "2019-06-20T23:29:22Z")

</div>

You can access julia’s parser through `Meta.parse` to get the correct object.

```julia
julia> eval(Meta.parse("Array{Float64}"))
Array{Float64,N} where N

```

However, I’d caution you that parsing and evaluating strings is in most cases _not_ the ideal way to achieve whatever it is you’re trying to do (though there are exceptions).

If you feel like explaining why you need to parse and evaluate a string to a datatype, perhaps we can advise on a safer and more performant option.

---

<div class="post-metadata">

**Author:** ![dataSurfer](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@dataSurfer](https://discourse.julialang.org/u/dataSurfer)\
**Post date:** [June 20, 2019, 11:40pm UTC](https://discourse.julialang.org/t/converting-string-to-datatype/25504/3 "2019-06-20T23:40:09Z")

</div>

That’s awesome thanks.

I’m writing something that can read/write any array type from/to binary files. I ran into some issues when trying to write `CatgegoricalArrays` to file. They have type element type `CategoricalString{UInt32}` which is where my code failed but your suggestion fixed it. I’m definitely open to suggestions on how to do this better.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 20, 2019, 11:46pm UTC](https://discourse.julialang.org/t/converting-string-to-datatype/25504/4 "2019-06-20T23:46:42Z")

</div>

As was mentioned in the thread you linked, if your program loads text data and blindly `eval`s it, you open any users of your code to attacks where someone can put malicious code into the data and have it be evaluated.

I’m no expert on this sort of stuff, but have you considered using [regular expressions](https://docs.julialang.org/en/v1/manual/strings/#Regular-Expressions-1) or just a simple dictionary to match strings from the file with types? That way there’s no arbitrary evaluation of code.
