Say I have a type:
public type Element = {
name: Text;
attributes: [(Text, Text)];
text: Text;
children: [Element];
};
I can create instances of this type by specifying values for all of the fields:
var el: Element = {
name = "my_name";
attributes = [("my_key0", "my_val0"), ("my_key1", "my_val1")];
text = "Some text";
children = [];
};
But what I really want Element to represent is an object that always has a name, and may have one or more of the rest of the fields, i.e. it may have a couple attributes and some text, but no children as in the above example or it may have no attributes and no text but a few children or some text but no attributes or children. I looked into accomplishing this structure using variants, but I really need an Element to represent any possible permutation, not just a select named few.
I’d like to create instance of Element like the example above but I don’t want to have to specify empty values for the fields I’m not interested in. I figured if I made the fields Optional, they would default to null if not specified, but this doesn’t seem to be how things work.
public type Element = {
name: Text;
attributes: ?[(Text, Text)];
text: ?Text;
children: ?[Element];
};
var el: Element = {
name = "my_title";
attributes = ?[("my_key0", "my_val0"), ("my_key1", "my_val1")];
text = ?"Some text";
};
results in
type error, expression of type
{attributes : ?[(Text, Text)]; name : Text; text : ?Text}
cannot produce expected type
{attributes : ?[(Text, Text)]; children : ?[Element]; name : Text; text : ?Text}
How can I create instances of Element by only specifying the fields I’m interested in, and have the non-specified fields default to null?