]> Cypherpunks repositories - gostls13.git/commit
fmt.Print*: reimplement to switch on type first.
authorRob Pike <r@golang.org>
Tue, 15 Jun 2010 00:16:35 +0000 (17:16 -0700)
committerRob Pike <r@golang.org>
Tue, 15 Jun 2010 00:16:35 +0000 (17:16 -0700)
commitd482c163be5ef83b9a4ff5b1f366ceb6cfff6ed7
treecd5819c95c79ba75273a44cefce075b84966312a
parent43b3a247d3471507da5df1f144ef12bda303dfe0
fmt.Print*: reimplement to switch on type first.
This shortens, simplifies and regularizes the code significantly.
(Improvements to reflect could make another step.)
Passes all.bash.

One semantic change occurs: The String() method changes
behavior. It used to run only for string formats such as %s and %q.
Instead, it now runs whenever the item has the method and the
result is then processed by the format as a string. Besides the
regularization, this has three effects:

1) width is honored for String() items
2) %x works for String() items
3) implementations of String that merely recur will recur forever

Regarding point 3, example from the updated documentation:
type X int
func (x X) String() string { return Sprintf("%d", x) }
should cast the value before recurring:
func (x X) String() string { return Sprintf("%d", int(x)) }

R=rsc
CC=golang-dev
https://golang.org/cl/1613045
src/pkg/fmt/fmt_test.go
src/pkg/fmt/format.go
src/pkg/fmt/print.go
src/pkg/fmt/scan_test.go
src/pkg/fmt/stringer_test.go
test/const3.go