Description
While debugging format's definition by visualizing the object tree, it's quite often helpful to have a concise string representation of the object. It's akin to #299, but that one is generated fully automatically, and this one is something that a format developer decides would be the best to display as a string.
WebIDE since the dawn of times had a WebIDE-specific key -webide-presentation
, which could be used on type level to show one-line long summary of the object even without opening its tree entry — but this facility is:
- WebIDE-only
- requires some substantial effort on visualizer side to support (effectively, reimplementing what compiler is already doing)
- makes it impossible to move that to other languages, as these expressions use specifically JavaScript-rendered names (i.e.
lowerCamelCase
instead oflower_underscore_case
).
KSY proposal
Add a type-level key to-string
, which will list a KS expression returning string. People are encouraged to add contents of various important attributes to this string.
Example:
seq:
- id: foo
type: strz
- id: bar
type: strz
to-string: |
"foo=" + foo + ", bar=" + bar
Of course, it looks super ugly compared to interpolated strings in -webide-representation
, so probably a templated/interpolated strings support in KS expression language would be beneficial.
Obviously, this is a separate task, so I will create separate issue for that.
Implementation
If present, string
will override "to string" / "repr"-style rendering generated automatically with #299, i.e. for Java example above will yield:
public String toString() {
return "foo=" + foo + ", bar=" + bar;
}
or Ruby will yield:
def inspect
"foo=" + foo + ", bar=" + bar
end