Converting a string to a type

284 views
Skip to first unread message

Tom Short

unread,
Oct 2, 2012, 10:46:31 AM10/2/12
to juli...@googlegroups.com
I know I've seen this before (probably on this list), but I can't find
it. How can I convert a string to a type?

e.g.: "Array{Int64,1}" to the type Array{Int64,1}

Stefan Karpinski

unread,
Oct 2, 2012, 10:50:01 AM10/2/12
to juli...@googlegroups.com
This will do it, but it's a bit dangerous for obvious reasons:

julia> eval(parse("Array{Int64,1}")[1])
Array{Int64,1}

The parse function returns two values: the parsed expression and the index after what it just parsed (to potentially continue parsing). Maybe the single-argument version of parse should automatically just return the parsed expression without the index since it isn't called with an index.


--




Tom Short

unread,
Oct 2, 2012, 11:13:48 AM10/2/12
to juli...@googlegroups.com
On Tue, Oct 2, 2012 at 10:50 AM, Stefan Karpinski <ste...@karpinski.org> wrote:
> This will do it, but it's a bit dangerous for obvious reasons:
>
> julia> eval(parse("Array{Int64,1}")[1])
> Array{Int64,1}

Thanks, Stefan. Is there a less dangerous method? My use case is
storing a type as an attribute string in an HDF5 file. Then, when the
file is read into Julia, the reader can dispatch on the Julia type of
an object. The reader needs to convert the string to a type.

Tim Holy

unread,
Oct 2, 2012, 11:41:37 AM10/2/12
to juli...@googlegroups.com
On Tuesday, October 02, 2012 11:13:48 AM Tom Short wrote:
> Thanks, Stefan. Is there a less dangerous method? My use case is
> storing a type as an attribute string in an HDF5 file. Then, when the
> file is read into Julia, the reader can dispatch on the Julia type of
> an object. The reader needs to convert the string to a type.

My email to the list this morning anticipated this question a bit. A safer
alternative might be

typestring = get_type(fid, object)
read_function = read_dictionary[symbol(typestring))]
myobj = read_function(fid, object)

That's not using multiple dispatch, of course. But it would avoid the problem
where someone created an object whose typestring was "run(`rm ~/*`)".

--Tim

Tim Holy

unread,
Oct 2, 2012, 11:45:24 AM10/2/12
to juli...@googlegroups.com
On Tuesday, October 02, 2012 10:41:37 AM you wrote:
> read_function = read_dictionary[symbol(typestring))]
> myobj = read_function(fid, object)
>
> That's not using multiple dispatch, of course

2 seconds of further thought suggests

T = type_dictionary[symbol(typestring)]
myobj = read(fid, T)

which is using multiple dispatch.

--Tim

Stefan Karpinski

unread,
Oct 2, 2012, 11:46:48 AM10/2/12
to juli...@googlegroups.com
I like that. It limits the possible types to a restricted set which seems reasonable.


--Tim

--




Tom Short

unread,
Oct 2, 2012, 11:50:56 AM10/2/12
to juli...@googlegroups.com
On Tue, Oct 2, 2012 at 11:45 AM, Tim Holy <tim....@gmail.com> wrote:
That seems like the best approach so far.

For the parse then eval approach, one could probably traverse the
expression to check for things that shouldn't be there.

Patrick O'Leary

unread,
Oct 2, 2012, 6:39:03 PM10/2/12
to juli...@googlegroups.com
On Tuesday, October 2, 2012 10:50:57 AM UTC-5, Tom Short wrote:
For the parse then eval approach, one could probably traverse the
expression to check for things that shouldn't be there.

One could, but this seems inherently fragile, since bad things can often be constructed in unexpected ways.

Tom Short

unread,
Oct 4, 2012, 3:02:55 PM10/4/12
to juli...@googlegroups.com
I ended up trying the parse/eval approach with checking. See here:

https://gist.github.com/3835660

Mark Vogelsberger

unread,
Oct 4, 2012, 4:46:03 PM10/4/12
to juli...@googlegroups.com
Hi again,

so this thread is getting longer than anticipated :).

Could someone look at my "bad" code above and tell me a "better" solution based on the discussion? I am trying to port large parts of Python code to Julia, so I need to get used to it a bit more...

Thanks!
Mark

Stefan Karpinski

unread,
Oct 4, 2012, 6:45:19 PM10/4/12
to juli...@googlegroups.com
Nice! I rather like that. Since it's essentially a whitelist approach, I think it's probably ok. Very cool that you can do things like this. It relies, of course, on the fact that parse and eval are separate steps (unlike in perl, python, etc.).
> --
>
>
>
Reply all
Reply to author
Forward
0 new messages