Julia supports dynamic "expando object" a la C#?

107 views Asked by At

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?

1

There are 1 answers

0
mbauman On BEST ANSWER

No, it is currently not possible to dynamically create new fields simply by assigning to undefined field names. It may be possible in the future with a feature like #1974: allow overloading of a.b field access syntax. Note, though, that it's currently tagged in the Julia 2.0+ milestone; it is not a high priority and may or may not ever happen.

It is possible to create a macro that changes field access into dict-like syntax, such that @expando contact.Name = "foo" is transformed into contact[:Name] = "foo", but it's probably better and easier just to use dictionaries and the indexing syntax directly.