The is "Expando objects" in Julia?


#1

In C# is possible to have a dynamic object with fields defined at runtime using expando objects. Example:

dynamic contact = new ExpandoObject();
contact.Name = “Patrick Hines”;
contact.Phone = “206-555-0144”;

As you can see, the contact object is dynamic, in the sense we can set fields in the object, without declaring previously the structure of the object. I understand in Julia we can have a parameter/variable without declared type, as in:

function setProp1(obj) obj.prop1=1 end

But, as i understand it, the obj object must be created from a type with a field prop1. If prop1 is not declared somewhere as a field, setProp1 can’t set dynamically the field prop1. If i want full dynamism, and i want not to worry for performance, there is a way to create “expando objects” in Julia?


#2

Use Dict.


#3

No, this isn’t possible with Julia types. The closest thing would be a dictionary from symbols to values, like:

contact = Dict{Symbol, String}()
contact[:Name] = "Patrick"
contact[:Phone] = "123-456"

If you really, really, really want to access the fields with dot notation, you could write a macro that transforms:

@expando contact.Name = "Patrick"

into the appropriate dictionary syntax:

contact[:Name] = "Patrick"

but this will make your code harder to follow (and won’t improve performance).


#4

Also, I can’t find much information about benchmarking ExpandoObject, but this blog suggests that their performance is similar to a dictionary anyway. That suggests that you won’t lose performance by switching to using a Dict in julia (except to the extent that Julia and C# may implement their dictionaries differently).


#5

Thanks for the detailed answer. Yes, i’m thinking in dot notation. And sorry for my English, i’m thinking in Dict semantic using dot notation, where i’m not worried by the performance.