Hi all,
I’m now playing around the new Base.getproperty method, which was introduced in the master branch of Julia very recently. It is cool, so I’m planning to use it in my packages.
The question is what will be the best practice for this new feature when the property of an object may be null. For example, assuming you write code to represent a tree structure consists of Nodes, you would like to add some properties like node.parent or node.nextsibling for traversal. However, these properties may be undefined when the node is a root node or the last sibling. In such a case, I think we can take one of two options:
- The return type of
Base.getpropertyis typed asNode. If you try to access an undefined property, it will throw some exception (e.g.ArgumentError). - The return type of
Base.getpropertyis typed asUnion{Node,Nothing}. If you try to access an undefined property, it will returnnothing.
If you choose the first approach, you will need another method to check the existence of the property a priori (e.g. node.hasparent). I think this is cumbersome so I’d like to take the second option. However, I’m not perfectly sure about it because this was supposed to be a bad part due to type instability. Also, the deprecation of cond?foo:bar syntax suggests a new feature is planned on the handling of nullable values.
Do we have a consensus on the interface of nullable properties?