-
Notifications
You must be signed in to change notification settings - Fork 18k
x/mobile: Java constructor proposal #17086
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
This is precisely what I have implemented as part of my proposal at #16876. I implemented automatic constructors for different reasons than you:
The instance identity only covers Go structs extending or implementing Java classes and interfaces. Generalizing instance identity to cover every Go (and Java) type doesn't (readily) work: Gomobile statically determines the proxy type from the call site, not the actual type of the argument or return value. So if you have a Go Preserving instance identity is attractive for several other reasons (fewer proxy allocations and the property that for equal Go instances, the proxies are also equal), so I haven't given up yet. However, I believe automatic constructors are not meaningful without some form of identity preservation. |
I hot this exact problem today. Creative g the constructors would be nice I feel |
#16876 is now in, and you should be able to create a cosntructor for a Go struct simply by embedding a Java class. For example
will make |
Though this would break building the same code for both android and ios. Perhaps by using build constraints? |
Ok, let's try it. I've mailed CL 29710 to expand support for generating constructors to every exported Go struct. |
CL https://golang.org/cl/29710 mentions this issue. |
A recent CL added Java constructors to generated classes that extends or implements other Java classes and interfaces. Constructors for a struct S are Go functions on the form func NewS...(...) *S If no such constructors exists, a default empty constructor is generated. Expand that to cover every exported Go struct. Fixes golang/go#17086 Change-Id: I910aba13d5884c3f67c946c62a8ac4a3db8e2ea7 Reviewed-on: https://go-review.googlesource.com/29710 Reviewed-by: David Crawshaw <[email protected]>
A recent CL added Java constructors to generated classes that extends or implements other Java classes and interfaces. Constructors for a struct S are Go functions on the form func NewS...(...) *S If no such constructors exists, a default empty constructor is generated. Expand that to cover every exported Go struct. Fixes golang/go#17086 Change-Id: I910aba13d5884c3f67c946c62a8ac4a3db8e2ea7 Reviewed-on: https://go-review.googlesource.com/29710 Reviewed-by: David Crawshaw <[email protected]>
I've been looking at the generated code for Android, and I think we could really make it a lot more natural by generating constructors for Java classes instead of relying on creator methods.
The idea behind my proposal is that we could simply detect specific function naming convention in Go, and if that holds, generate a constructor instead of a static method.
Basic constructors
If I were to have a type on Go side:
Then either of the functions below could be interpreted as a constructor for
SomeType
:Instead of generating a static method in the root namespace, we could generate a public constructor:
The rule would be that if a Go package contains both TypeXXX and a function NewTypeXXX which returns TypeXXX and optionally an error, we can interpret that as a constructor.
Overloaded constructors
An even more interesting (alas less obvious) case are overloaded constructors. Many times it can happen that we need to create a new "object" from multiple possibly input types.
One example of such is Go's
binary
package:In this case the "Basic" approach above would generate a constructor from the first, but not the second function. I'd propose that all functions called
NewTypeXXX<whatever>
that have the required signature (returns a pointer to a type and an optional error) is used to generate overloaded constructors.Clashes
Of course, since we're "merging" different named methods under the same constructor name, there's nothing stopping me from defining multiple methods in Go that would en up with the same constructor signature.
In that case we can just fall back to the current behavior of retaining the methods and not creating constructors out of them.
I'd be happy to code this up if there's interest. @eliasnaur ? :)
The text was updated successfully, but these errors were encountered: