Skip to content

Question: Pointers with js tags #504

Open
@flimzy

Description

@flimzy

Question:

Is code like the following expected to work?

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions