-
Notifications
You must be signed in to change notification settings - Fork 18k
syscall/js: unclear what the zero js.Value represents #27592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yes, currently the zero |
Choosing one option so it's clear what I'm 👍-ing:
Agreed. |
I have a relevant mini-experience-report to share. In the WebGL 1 spec, two of the functions are defined as follows: WebGLProgram? createProgram()
void useProgram(WebGLProgram? program) The
This is sometimes used during development to reset state after a program is done being used, to avoid accidentally using it elsewhere. E.g.: gl.useProgram(program)
// use program
gl.useProgram(null) I was working on porting some cross-platform We know it's a good practice in Go to make the zero value useful. So, in the program := gl.CreateProgram()
if !program.Valid() {
return fmt.Errorf("no programs available")
}
gl.UseProgram(program)
// use program
gl.UseProgram(gl.Program{}) Inside the type Program struct {
program *js.Object
}
func (p Program) Valid() bool { return p.program != nil }
func UseProgram(p Program) {
c.Call("useProgram", p.program)
} If I were to implement the WebAssembly version analogously, it would look like this: type Program struct {
program js.Value
}
func (p Program) Valid() bool { return p.program != js.Null() }
func UseProgram(p Program) {
c.Call("useProgram", p.program)
} One immediately obvious problem with that is that the zero The other problem is that
So I'd actually need to use the following code as a work around: type Program struct {
program js.Value
}
func (p Program) Valid() bool { return p.program != js.Null() && p.program != js.Value{} }
func UseProgram(p Program) {
// Workaround for js.Value zero value.
if p.program == (js.Value{}) {
p.program = js.Null()
}
c.Call("useProgram", p.program)
} It seems that these complications would go away if the zero value of |
I think the WebGL usage (and your description) corresponds well with: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null#Description But I think the primitive value
i.e. it is the "default" value in all situations in JavaScript. By contrast, |
Change https://golang.org/cl/143137 mentions this issue: |
Change https://golang.org/cl/154618 mentions this issue: |
Also update a Go 1 compatibility promise link to canonical URL. Updates #27592 Updates #28264 Change-Id: I5994a0a63e0870c1795c65016590dfad829d26a7 Reviewed-on: https://go-review.googlesource.com/c/154618 Reviewed-by: Richard Musiol <neelance@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
The godoc of
js.Value
type currently says:It's not easy to figure out what the zero value represents, even after reading the rest of
syscall/js
docs.This question came up when I had a function with the return types
(js.Value, error)
and I wanted to figure out whatjs.Value
to return when an error happened. Typically, one returns the zero value, but it helps to understand what the zero value of that type represents.From looking at the implementation, I understand that the zero
js.Value
value currently represents the JavaScript number zero, is that right @neelance? I.e.:It's also possible that what the zero
js.Value
value represents is considered an internal implementation detail and not meant to be a part of the public API. Hence, users shouldn't rely on it to have any well-defined and stable meaning.I think it would be helpful to document what the zero value represents, so people can answer this question by reading the documentation. Depending on the intention, perhaps one of these:
(See https://godoc.org/math/big#Int documentation for a similar example.)
Or (an improved version of):
/cc @neelance
The text was updated successfully, but these errors were encountered: