-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
can't bind golang generic type #3715
Comments
I bump this heavily. I attempted to do the same thing with attempting to create a generic result object to bypass the lack of good error handling or multiple return type support in wails. type Error struct {
Message string `json:"message"`
}
type GenericResult[T any] struct {
Data *T `json:"data,omitempty"`
Error *Error `json:"error,omitempty"`
}
func (s *StudentController) GetStudent(id string) GenericResult[Student] {
...
} Results in: export function GetStudent(arg1:string):Promise<domain.GenericResult[freegrad/domain>;
This kind of thing aligns with the enhancement request for error handling #3301. While Wails does not allow for returning multiple return types, we are forced into this single object result type to make things clean, or at least attempt to without having to create a unique return type for each function. However, without properly handling generics this way, we are forced to do just that instead of a single generic return type that can be used across the entire application. Is this something that can be address in the v3 release? I would be willing to take a stab at it in that case but would like more feedback and discussion first. |
It also seems that there is a dangling PR related to this issue that may be a fix #3626 ? |
There are 2 distinct things you are bringing up here:
I personally believe that if you are trying to call methods on nil pointers it SHOULD crash. This is standard for Go. If that's the only thing you're guarding against then it creates a wildly cluttered API whose only usefulness is to guard you against yourself in whichever is the first method you call in the application. If it can error, and we don't bubble that back up, then sure. In general though, UI errors are catastrophic system errors and there's nothing you can do about them.
We are totally open to supporting this and welcome anyone with the time and knowledge to do it. V2 uses a third party library which now supports this. It just needs someone to look at porting the new one over and adding in the Wails custom code, or refactoring to use it in a different way so that the module can be used directly (if possible). V3 uses a static analyser to understand your code and generate bindings. It also does not support generics at this point, but there is an if statement waiting to be filled in with the appropriate code. Perhaps @fbbdev can advise. |
On point one, I am not talking about calling method's on nil pointers or anything. Indeed this would be a crash and not something that should casually bubble up the frontend. I'm really just talking about exposing a go type error to the frontend, which really has to do more with returning more than one type from any backend API call. The ability to use generics with this case would help alleviate the overhead of creating a bunch of wrapper return types to expose an error on the backend. I can see this being an implementation decision for each project but the reality is that being limited to one return type is a bit of bottleneck. I'll take a look at the code for v3 and see if I can figure out how generics can work in the stub you are mentioning. |
When you say "only limited to one return type" what do you mean exactly? You have 2: a result and an error. Perhaps I'm misunderstanding what you're saying. The error is returned to the frontend via a promise rejection. |
Well in that case it might be my misunderstanding. From what I gather, a golang |
As regards v3, multiple return values and generics are already fully supported up to Go 1.23. Some minor changes will be required to support generic aliases that should be coming with Go 1.24. Return values of type A bridge between JS and Go must necessarily be unidiomatic to some extent, as the two languages adopt incompatible idioms: the current approach favours JS-style error handling (hence either normal return values or exceptions, but not both). Error values are marshaled somehow and passed on as the rejection value/exception. I don't recall the exact details and can't check the code right now. Anyways, all this will have to be detailed in v3 docs as we enter the beta phase. |
So regarding the Also what is the type of |
I've had a look at the code, here's the details. I hope this clears things up a bit. Better documentation is definitely needed. Wails v2The binding engine supports either exactly one return value or exactly two return values where the 2nd one implements the If an error formatting function is provided among application options (field If no error formatting function is provided, then non-nil errors are converted to strings by calling the standard Wails v3The binding engine supports arbitrarily many return values. Return values whose exact type is If any one of the potential error values is non-nil, all of them are joined in order of declaration using Re: v3. We definitely can, and probably should, do better here @leaanthony. When errors are returned from user code, as opposed to being raised internally by wails, we could try and marshal them to JSON directly instead of formatting them with the aforementioned template. If marshaling fails, we could then reject with the standard error string, without any additional decorations. We should use Also, we could introduce a special exception class and name for internal errors, to make them easily distinguishable from user-defined API errors. Thoughts? |
We had a v2 PR for a custom error handler for bindings which is an option. |
It's covered here: https://wails.io/docs/howdoesitwork#calling-bound-go-methods |
Description
golang type:
bind ts type:
To Reproduce
none
Expected behaviour
bind correct
Screenshots
No response
Attempted Fixes
No response
System Details
Additional context
No response
The text was updated successfully, but these errors were encountered: