You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package bug
func (p *Gray) Set(x, y int, c color.Color) {
// Discard pixels outside the image.
if !(image.Point{x, y}.In(p.Gray.Rect)) {
return
}
p.Gray.Set(x, y, c)
p.SetBraille(x, y, p.ColorModel().Convert(c))
}
Overrides is in 'quotes' because Go isn't really object-oriented the way C++ or Java is.
Note that bug.Gray's Set method calls the embedded image.Gray's Set method and does other things - it calls SetBraille.
Now, what happened in Go 1.18 is that there's new image.RGBA64Image and draw.RGBA64Image interfaces and that image/draw will use them (if your destination image implements that interface).
With Go 1.18 (and later), a bug.Gray automatically implements the draw.RGBA64Image interface (even though the package bug source code hasn't changed) because the embeddedimage.Gray implements this interface. But bug.Gray doesn't 'override' SetRGBA64, so when image/draw calls SetRGBA64, it doesn't do the other things - it doesn't call SetBraille.
@ianlancetaylor and others can correct me if I'm wrong, but I don't actually think this can be 'fixed' in the standard library, if we still want to keep the RGBA64Image optimizations that landed in Go 1.8.
Instead, I think that package bug needs to change, either by (1) not actually using embedding or (2) explicitly having bug.Gray implement SetRGBA64. Or both, but you only need at least one of those two.
Personally, I think that, in hindsight, Go's embedding feature is a mistake and methods should instead always be explicitly 'forwarded' to struct fields. Especially as doing so would avoid surprises like this. I would recommend (1), like this:
@@ -66,7 +66,7 @@ func (cm Threshold) Inverse() Threshold {
// Each braille character represents 2x4 actual pixels.
type Gray struct {
// real holds the real pixel version of the image.
- *image.Gray+ Gray *image.Gray
// content holds the braille representation of the image.
// Not using stdlib's single dim slice as benchmark shows
@@ -108,6 +108,9 @@ func NewGray(r image.Rectangle) *Gray {
return img
}
+func (p *Gray) At(x, y int) color.Color { return p.Gray.At(x, y) }+func (p *Gray) Bounds() image.Rectangle { return p.Gray.Bounds() }+
// Clear all pixels.
func (p *Gray) Clear() {
// The for loop is more efficient that a copy of empty element.
This patch makes the package bug tests pass for me.
Thanks for the analysis. I agree that this is not a bug in Go. The Go compatibility guarantee permits us to add methods to existing types. Any embedding of a type in the standard library must consider this possibility.
Activity
gabyhelp commentedon Sep 30, 2024
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
randall77 commentedon Sep 30, 2024
It would be helpful to bisect to a single CL if you can do that.
There was an image/draw change in 1.18, see https://tip.golang.org/doc/go1.18#minor_library_changes. Not sure if that is related to your code or not.
ianlancetaylor commentedon Sep 30, 2024
The behavior change was indeed caused by https://go.dev/cl/340049. CC @nigeltao
[-]images: breaking change introduced in 1.18[/-][+]image/draw: blank images after go1.18[/+]nigeltao commentedon Oct 4, 2024
Here's what's happening.
bug.Grayembeds a from-the-standard-libraryimage.Gray:It also 'overrides' the Set method:
Overrides is in 'quotes' because Go isn't really object-oriented the way C++ or Java is.
Note that
bug.Gray'sSetmethod calls the embeddedimage.Gray'sSetmethod and does other things - it callsSetBraille.Now, what happened in Go 1.18 is that there's new
image.RGBA64Imageanddraw.RGBA64Imageinterfaces and thatimage/drawwill use them (if your destination image implements that interface).With Go 1.18 (and later), a
bug.Grayautomatically implements thedraw.RGBA64Imageinterface (even though thepackage bugsource code hasn't changed) because the embeddedimage.Grayimplements this interface. Butbug.Graydoesn't 'override'SetRGBA64, so whenimage/drawcallsSetRGBA64, it doesn't do the other things - it doesn't callSetBraille.@ianlancetaylor and others can correct me if I'm wrong, but I don't actually think this can be 'fixed' in the standard library, if we still want to keep the
RGBA64Imageoptimizations that landed in Go 1.8.Instead, I think that
package bugneeds to change, either by (1) not actually using embedding or (2) explicitly havingbug.GrayimplementSetRGBA64. Or both, but you only need at least one of those two.Personally, I think that, in hindsight, Go's embedding feature is a mistake and methods should instead always be explicitly 'forwarded' to struct fields. Especially as doing so would avoid surprises like this. I would recommend (1), like this:
This patch makes the
package bugtests pass for me.ianlancetaylor commentedon Oct 4, 2024
Thanks for the analysis. I agree that this is not a bug in Go. The Go compatibility guarantee permits us to add methods to existing types. Any embedding of a type in the standard library must consider this possibility.