# Convert String to DataType

**URL:** https://discourse.julialang.org/t/convert-string-to-datatype/23803
**Category:** General Usage
**Created:** [May 3, 2019, 8:06am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803 "2019-05-03T08:06:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)
#### Post date: [May 3, 2019, 8:06am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/1 "2019-05-03T08:06:39Z")

</div>

I have a file that contains a List of what “Algo” I should run  
Then Julia should read it and Create an instance of the correct “Algo” object:

```julia
abstract type OptimizationAlgo end
struct Linear <: OptimizationAlgo end
struct Quad <: OptimizationAlgo end
struct ThirdOrder <: OptimizationAlgo end

algo_str = readline() # Let's say I type in "Linear"

# Convert algo_str to the correct algo struct
# How to do it??

```

But how to efficiently do so?

The only way I could think of:

`@eval $(Symbol(algo_str))`

**Is there a way Without Using any Macro?**

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [May 3, 2019, 8:14am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/2 "2019-05-03T08:14:07Z")

</div>

i can’t think of another solution of that other that a long function with a lot of ifs, maybe that is the correct approach, because the algorithm is not known a priori (before reading the file)

---

<div class="post-metadata">

### Author: ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)
#### Post date: [May 3, 2019, 8:24am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/3 "2019-05-03T08:24:20Z")

</div>

Is there a way Without Using any Macro?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [May 3, 2019, 8:34am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/4 "2019-05-03T08:34:58Z")

</div>

If the number of algorithms is limited you can use a dictionary as a mapping from string to object.

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [May 3, 2019, 8:35am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/5 "2019-05-03T08:35:36Z")

</div>

You can short circuit equality for string such as

```julia

julia> algo_str = readline()
Linear
"Linear"

julia> algo_str == "Linear" && Linear()
Linear()

```

or you can construct a dictionary from strings to algos if the list of algos is long

```julia
julia> b = Dict("Linear"=>Linear())
Dict{String,Linear} with 1 entry:
  "Linear" => Linear()

julia> b[algo_str]
Linear()

```

But second one has the problem of type-stability if more algos is mixed in dictionary.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [May 3, 2019, 8:38am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/6 "2019-05-03T08:38:46Z")

</div>

If you don’t want to use a macro, `getproperty(Main, Symbol(algo_str))` does the job.

---

<div class="post-metadata">

### Author: ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)
#### Post date: [May 3, 2019, 8:41am UTC](https://discourse.julialang.org/t/convert-string-to-datatype/23803/7 "2019-05-03T08:41:13Z")

</div>

Thank you! problem solved.
