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
{{ message }}
Repository navigation
No nice way to set CanvasRenderingContext2D.FillStyle to gradient, pattern. #45
This works, but interface{} isn't great for APIs. Any other type can be assigned too, and when querying the value of FillStyle, you get an interface{} which isn't nice either.
Possible solution 3
Define 3 fields of 3 different types, all mapping to JS property fillStyle:
Option 2 would be okay only if you document what should go into the interface, i've seen some packages wich don't do that and leave what should go into the interface to the imagination of who uses them.
Option 3 would be more verbose and everyone will immeditely be able to see the function signature directly from the ide:
I used this solution for the functions DrawImageDrawImageWithDstDrawImageWithSrcAndDst.
In that case the only difference was just the number of arguments, but i don't think having more than version of the same function is an issue, is probably still better than to use reflection with the interfaces.
A combination of 2 and 3 could also be done, so both having a generic version of the function wich just takes an interface and the typed versions like FillStyleWithColorFillStyleWithGradientFillStyleWithPattern .
At then end of the day there's not really anything better we can do.
I oppose option 3. It's akin to unions, a data type that is not available in traditional Go, and for a good reason.
In particular, I don't want code like this to occur:
ctx.FillStyle = "rgb(255, 0, 0)"
ctx.FillStyleGradient = gradient
fmt.Println(ctx.FillStyle) // why is this magically not "rgb(255, 0, 0)" anymore? Nothing set that field!
Option 1 is also not a valid choice. If the user has to use the low level js.Object API, that's identical to our package lacking a feature.
I offer another option for consideration: discriminated unions by using an interface. Something like this:
type FillStyle interface {
isFillStyle()
}
type StringFillStyle string
func (StringFillStyle) isFillStyle() {}
// do the same for gradient and pattern, which are conveniently already declared types
func (ctx) SetFillStyle(fs FillStyle) {
// set the fill style
}
Activity
luckcolors commentedon Oct 20, 2017
I would suggest we either use option 2 or 3.
Option 2 would be okay only if you document what should go into the interface, i've seen some packages wich don't do that and leave what should go into the interface to the imagination of who uses them.
Option 3 would be more verbose and everyone will immeditely be able to see the function signature directly from the ide:
I used this solution for the functions
DrawImageDrawImageWithDstDrawImageWithSrcAndDst.In that case the only difference was just the number of arguments, but i don't think having more than version of the same function is an issue, is probably still better than to use reflection with the interfaces.
A combination of 2 and 3 could also be done, so both having a generic version of the function wich just takes an interface and the typed versions like
FillStyleWithColorFillStyleWithGradientFillStyleWithPattern.At then end of the day there's not really anything better we can do.
dominikh commentedon Oct 20, 2017
I oppose option 3. It's akin to unions, a data type that is not available in traditional Go, and for a good reason.
In particular, I don't want code like this to occur:
Option 1 is also not a valid choice. If the user has to use the low level js.Object API, that's identical to our package lacking a feature.
I offer another option for consideration: discriminated unions by using an interface. Something like this: