Default Properties

Normally, when you read the value of a nonexistent property, DScript returns null. However, you can create a default property using the @ symbol that is returned whenever a matching property is not found.

For example, the following code shows how the consolidate primitive is defined. Consolidate is used to group a set of component objects into a super-object where calling a property in the super-object returns the sum of all values in all child objects.

consolidate(objects,...):=new _consolidate(arguments)
_consolidate(obj):={
    .objlist=obj;
    _consolidate.prototype.@=_consolidate @;
}
_consolidate @():={
    if(propname=="inputs")
        null;
    else if(propname=="outputs")
        union(flatten(user(each(x.outputs,x,.objlist))));
    else 
        sum(user(each(x[propname],x,.objlist)));
}

You use this primitive by passing a list of object to the consolidate function. This returns a new object with only two properties, objlist and @. Objlist contains a list of all object passed to consolidate and @ is the default, catch-all property. So, if you were to call the "Total Costs" property of the new super-object, consolidate would sum up the values of all "Total Cost" properties of all objects in objlist.

You can use the propname primitive to retrieve the name of the property chosen by the user.

See propname.

See Access Object Properties.