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
type X struct {
*js.Object
Baz *string `js:"baz"`
}
Of course the reason I ask is that it's not working. But then, I'm not really sure what I would expect it to do. So it seems quite plausible that it's not meant to work. In that case, perhaps a more graceful error message would be my feature request.
At the moment, if I attempt to assign something to X.Baz, then try to retrieve it, I get:
TypeError: Cannot assign to read only property '0' of [object String]
And if I try to read from it before it's initialized, I get:
TypeError: Cannot read property 'constructor' of undefined
Which is the same behavior I get if I attempt to read an uninitialized array with a js tag. But I can initialize the array first, and then things work.
My test code is as follows:
package main
import (
"fmt"
"github.com/gopherjs/gopherjs/js"
)
type X struct {
*js.Object
Baz *string `js:"baz"`
}
func main() {
o := js.Global.Get("Object").New()
x := &X{Object: o}
// Uncomment the following two lines to attempt assignment
// var oink string = "oink"
// x.Baz = &oink
fmt.Printf("x = %s\n", *x.Baz)
}
The text was updated successfully, but these errors were encountered:
It's definitely a bug/issue if it's not documented and doesn't work. We should fix it either via documentation and more graceful error message, or by actually implementing it.
I haven't looked to closely at this yet, I plan to do that sometime. At a glance, I suspect we shouldn't try to support this... But I might be missing important details.
Is there a good reason to use pointers rather than values for fields? I'm only asking to understand this issue better.
The reason I was experimenting with it is that I wanted to use nullable values in a struct. Of course, JS objects already support this in a way that Go does not, as JS distinguishes between undefined and false, undefined and 0, undefined and "", etc.
Ultimately, in my case, I decided that the cleanest solution was a "pure" Go struct (as in, no embedded *js.Object, and no js tags), with a .compile() method which does an intelligent conversion to a map[string]interface{} to be passed to the JS method I was wrapping.
I haven't spent a lot of effort thinking about what I would expect should happen with pointers + js tags, or whether there is a legitimate use case for it. Although I haven't tested, I expect something similar would happen with an interface with a js tag, which might make sense in some cases, such as:
type Foo struct {
*js.Object
Error error `js:"error"`
}
Uh oh!
There was an error while loading. Please reload this page.
Question:
Is code like the following expected to work?
Of course the reason I ask is that it's not working. But then, I'm not really sure what I would expect it to do. So it seems quite plausible that it's not meant to work. In that case, perhaps a more graceful error message would be my feature request.
At the moment, if I attempt to assign something to X.Baz, then try to retrieve it, I get:
And if I try to read from it before it's initialized, I get:
Which is the same behavior I get if I attempt to read an uninitialized array with a js tag. But I can initialize the array first, and then things work.
My test code is as follows:
The text was updated successfully, but these errors were encountered: