toString method

String toString()
override

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation

String toString() {
  var buffer = StringBuffer();

  // dart-lang/language#3064 and #3062 track potential ways of making this
  // cleaner.
  var leftNeedsParens = switch (left) {
    BinaryOperationExpression(operator: BinaryOperator(:var precedence)) =>
      precedence < operator.precedence,
    ListExpression(hasBrackets: false, contents: [_, _, ...]) => true,
    _ => false
  };
  if (leftNeedsParens) buffer.writeCharCode($lparen);
  buffer.write(left);
  if (leftNeedsParens) buffer.writeCharCode($rparen);

  buffer.writeCharCode($space);
  buffer.write(operator.operator);
  buffer.writeCharCode($space);

  var right = this.right; // Hack to make analysis work.
  var rightNeedsParens = switch (right) {
    BinaryOperationExpression(:var operator) =>
      operator.precedence <= this.operator.precedence &&
          !(operator == this.operator && operator.isAssociative),
    ListExpression(hasBrackets: false, contents: [_, _, ...]) => true,
    _ => false
  };
  if (rightNeedsParens) buffer.writeCharCode($lparen);
  buffer.write(right);
  if (rightNeedsParens) buffer.writeCharCode($rparen);

  return buffer.toString();
}