Search results

There are no results.

std.fmt.ObjectFormatter

type pub ObjectFormatter

A type for making it easy to format regular objects.

Instance methods

field

Show source code
Hide source code
fn pub mut field[T: Format](
  name: String,
  value: ref T,
) -> mut ObjectFormatter {
  let start = match @fields {
    case 0 if @named -> '('
    case 0 -> '('
    case _ -> ', '
  }

  @formatter.write(start)
  @formatter.write(name)
  @formatter.write(': ')
  @formatter.descend(fn { value.fmt(@formatter) })
  @fields += 1
  self
}
fn pub mut field[T: Format](name: String, value: ref T) -> mut ObjectFormatter

Adds a new formatted field to the output.

finish

Show source code
Hide source code
fn pub mut finish {
  match @fields {
    case 0 if @named -> {}
    case 0 -> @formatter.write('()')
    case _ -> @formatter.write(')')
  }
}
fn pub mut finish

Finishes formatting the object.

This method is used instead of a Drop implementation, otherwise a call chain (e.g. x.object('A').field('foo', 10)) results in a drop error, as the final reference returned by field would outlive the ObjectFormatter.