Constructor parameters for JVM-defined GObjects beyond MemorySegment #90
Labels
No labels
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
java
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
java-gi/java-gi#90
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Currently the only constructor parameter a GObject subclass can have is the
MemorySegmentrequired for FFI.Idiomatic Scala would prefer to have other parameters in the constructor; "null on construction and mutably updated later" to work around this is very unidiomatic in Scala as both
nulland mutable variables aren't favourably looked upon in most Scala code.I'm not sure I understand what you mean. Can you explain what sort of API you'd expect?
Some explanation about the current situation:
Java-GI creates JVM "proxy" instances for all native GObject instances that are used from the JVM side. It will call the
MemorySegmentconstuctor of the JVM class when a native function returns an object for which a JVM proxy doesn't exist yet.To prevent multiple JVM proxy objects for the same native GObject instance, Java-GI maintains an internal HashMap with all proxy objects. This also allows you to add your own instance fields, pass it to native code, receive it back later, and it's still the same instance. Only when the native GObject instance is disposed, the instance in the Java-GI HashMap is removed.
The
MemorySegmentconstructor is called by Java-GI when a native function returns a GObject instance and a JVM proxy object doesn't exist for it yet. Additional constructor parameters wouldn't make sense there, because the native function only returns a pointer to the existing GObject, so that's all we can pass to a constructor.However, you can add a second constructor of your own, to create brand new instances of your own classes. That constructor will not have a
MemorySegmentparameter, because a native GObject doesn't exist yet. So you can do something like this:However, this will not work if you have a custom GObject instance initializer method (using an
@Initannotation). If you want to do that, you must use a static factory method that callsGObject.newInstance(gtype). That will create the new instance, call its initializer method, and return that instance. A regularsuper()constructor call cannot do that, which is why I recommend against this in the documentation.@pontaoski Can you please explain a bit more what the expected API would look like?
No response, closing