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
A component nested in another as a pointer is always nil
#853
Comments
Hm, this seems similar to #559 |
As documented, You need to initialize its values in |
Ow. Thanks for explaining and the doc pointer. I now understand better what is going on. But now to me it seems like a bad API, since it is easy to use incorrectly and astonish the user. A good API should make incorrect use impossible. Some options.
Either way, you don't even need to document the peculiar behavior where if you do app.Route("/", &someStruct), you actually don't get to use |
It needs to be the correct type, though. You cannot hand over The The right place to initialize your components actually should probably always be That you want to initialize your component before the route is called and therefor used, is also usually not very likely. Think of having 10 routes. You do not want to have all of these components being values. You may want to read #730 and check out "mountpoint" (and the example) from https://github.com/metatexx/go-app-pkgs P.S.: It is also a problem you have just once in a life time :) |
Sure you can: https://go.dev/play/p/SYSjyVvp1ui The idea here is to prevent the user from inadvertently passing in
Absolutely. And I'm happy to give credit where credit is due. I still think there is room for API improvements nevertheless.
You are probably right. But this is not relevant from the perspective of the
Agreed; however, it's still a good idea to prevent API misuse. To wit, it's easy to know this in principle. It is a different thing to be cognizant of the issue at all times, especially when (1) there may be hundreds of components you wrangle; (2) the composition pattern is the fundamental idea to software engineering and should work correctly all the time; and (3) the resulting errors can be really confusing, since if you refer to an internal pointer field you get something like Lots of footguns above. Avoid footguns.
If you can have it zero times in a lifetime, it would surely be better? |
Well, that is a clever trick with the (limited) generics support and does not look very appealing to me. Even this does not take a type anyway. It takes an interface with a function that returns a typed nil value. And to explain it to a user, you need to explain that you made it harder to give a component in the route because people failed to read the documentation. I am uncertain if I advocated this.
Sure, and I did the same error as you (not reading the documentation of functions I used). As I said, I think the example could be clearer to point it out that you just give an example of the type. But I am not @maxence-charriere, and he will have his own idea about it, he always has :) |
I suppose so. Prior to reporting this bug, I stared at that sentence in the original documentation and I didn't understand what it meant. Not until you provided the context.
Fair point. There's enough material here to weigh the pros and cons of the issue, and I'm happy to leave it at that. I remain grateful that this project exists. |
I stepped on this while trying to migrate this piece of software from go-app v6 to v9.
https://github.com/grahamjenson/bazel-golang-wasm-proto/pull/4/files#diff-2afb4d11c8069bc5fb58b2d00e2e0c9c3cb8b8bb926bf648ed7b95d856655af7R14
In short, components are declared as follows (details in the PR):
The code gets initialized in main.go:
What I observe on the browser side is that, despite calling
SetManager(...)
on bothSearchBar
andInstanceTable
, the value of their respective.Manager
field is alwaysnil
. Logging some addresses ofSearchBar
andInstanceTable
, I noticed that the addresses of the respective instances are not the same as the addresses of the instances composed intoManager
.This of course fails with a
nil
dereference, whenSearchBar
tries to refer to itsnil
manager. See https://github.com/grahamjenson/bazel-golang-wasm-proto/pull/4/files#diff-a2c869579f3c6ab043908c5904a5dfd0264e7b9717e08c14edac23a29b03cdf9R43It seems as if instances of
SearchBar
andInstanceTable
are being copied even if I don't want them to be copied, and the respective pointers aren't copied. Any ideas why this is happening? This is only an issue in v9, it seems that v6 is fine with the general approach.It's not out of the question that I'm doing something wrong. The example apps in
go-app
don't treat this at all- there's just a basic "hello world", and others don't haveapp.Compo
hold pointers to other UI elements. What gives?Thank you for looking at this!
The text was updated successfully, but these errors were encountered: