# Parse String to DataType

**URL:** <https://discourse.julialang.org/t/parse-string-to-datatype/7118>\
**Category:** General Usage\
**Tags:** question, strings\
**Created:** [November 17, 2017, 8:35am UTC](https://discourse.julialang.org/t/parse-string-to-datatype/7118 "2017-11-17T08:35:50Z")\
**Posts on this page:** 1\
**Showing post:** 12

<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:** [July 26, 2018, 8:57pm UTC](https://discourse.julialang.org/t/parse-string-to-datatype/7118/12 "2018-07-26T20:57:09Z")

</div>

> [@jandehaan](#):
>
> How about

There’s no way around `eval` if you want to handle arbitrary types. Many people seems to think `macro` gives you magic power to accomplish things (edit: at runtime) that are not possible without them. They don’t, they are just a fancy way to save repeated typing.

Going from string to value is not parsing, its evaluating (ok, parsing (to AST) plus evaluating (to value)). The only way to evaluate something from a runtime expression is `eval`, everything else is just `eval` wrapped in one way or another, including `include`, `include_string` etc.

The real question is what property you want. Do you want to handle all possible types? If yes, then you have to use `eval`. Do you want to avoid code injection attach? Then you can’t use `eval`, you can use a table (`Dict`) or use an arbitrary set of operations you support and are safe for your application.

> [@galenlynch](#):
>
> since they’re such an important part of Julia.

They are not. Not for the parser and not any more than basically anything else. Types are values, they are special in dispatch but that has nothing to do with syntax/parsing. Designing a system that give them more coupling is possible (C++…) but that’s usually not a desired property and makes parsing so much harder.

> [@galenlynch](#):
>
> I don’t really understand why `DataType` isn’t already a parse target

As an example, what do you think the last `B` in the following code should parse to?

```julia
a = 2 + 2
struct B
    x::NTuple{a,Int}
end
B

```

It’s obviously a type (which is clear in this case but does not have to in general in julia) but how do you want the parser to figure out all it’s property before the code is evaluated?

---

_[View the full topic](https://discourse.julialang.org/t/parse-string-to-datatype/7118)._
