-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: improve handling of wrapped interface values
This test (assert2.go) display 2 separate issues: 1. assert2.go L28: Type assert tries to set an `interface{}` to a `valueInterface`. The typing here is complex, we have a valueT(strings.Builder) wrapped in a ptrT wrapped in a src iface wrapped in a valueT(interface{}). Type assert fails to realise that the `valueT` `interface{}` is wrapping the `valueInterface`. 2. assert2.go L29: `genValueBinMethodOnInterface` does not try and get the bin method, as the `typ.node` (`ptrT` or a `valueT`(`string.Builder`)) is set. In this case the src iface is called with a receiver argument. To fix this the method is looked for first if possible, and only if not found does it fall back to the `defaultGen`. Fixes #1227
- Loading branch information
Showing
3 changed files
with
46 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// Defined an interface of stringBuilder that compatible with | ||
// strings.Builder(go 1.10) and bytes.Buffer(< go 1.10) | ||
type stringBuilder interface { | ||
WriteRune(r rune) (n int, err error) | ||
WriteString(s string) (int, error) | ||
Reset() | ||
Grow(n int) | ||
String() string | ||
} | ||
|
||
var builderPool = sync.Pool{New: func() interface{} { | ||
return newStringBuilder() | ||
}} | ||
|
||
func newStringBuilder() stringBuilder { | ||
return &strings.Builder{} | ||
} | ||
|
||
func main() { | ||
i := builderPool.Get() | ||
sb := i.(stringBuilder) | ||
_, _ = sb.WriteString("hello") | ||
|
||
println(sb.String()) | ||
|
||
builderPool.Put(i) | ||
} | ||
|
||
// Output: | ||
// hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters