-
Notifications
You must be signed in to change notification settings - Fork 137
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
Implement java.lang.AutoCloseable in class org.eclipse.swt.graphics.GC #955
Comments
+1 |
Does anybody see unwanted side-effects when we would implement this proposal? |
I tried it locally a few minutes ago and checked if existing uses would get a warning about missing the close. New warnings that suggest close must be called would be very annoying... |
And did new warnings happen? |
Understood. We would introduce warnings or errors for existing code which does call dispose, but not using try-with-resources. We want to find all the locations which do not call dispose and introduce a SWT handle leak. Or make it easier for consumers so that dispose() must no longer be called explicitly. |
I fully understand the common/general use case of a GC needing to be disposed within the block of code in which it is created and see significant value in the suggestion from that point of view. It's just that when we talk about what "we" want, we ought to take into consideration everyone. Definitely not everyone will want new errors, warnings, or infos. Also, a great many of the "we" out there will want to provide libraries that work with older versions of SWT as well as with the latest version of SWT. This makes retrofitting good new patterns into the existing code a significant tradeoff for the huge downstream code base out there in the ecosystem. |
Agree that it's a great idea but, like @merks, I implemented |
There are several ways to accomplish something similar for example one can have a static method in GC class like this:
|
In general I like to leverage java language to increase the safety of the program and
Do you have thrid-party code as sources in your workspace? Because for compiled jars such warnings are not shown. Maybe it would then be good to contribute to those projects (if they are open source).
I would name it
I think nobody wants that, but applying a new method/class/etc. to the Eclipse SDK code base is often a good measure to see if it is really as good as hoped and to find potential flaws early. So ideally the designer also takes up that burden. :)
This is indeed a real difficulty and I understand that it is annoying for those that can't use the new things. But how do we then tell those that can use the new methods that there is something better to use? I doubt that that many scan the docs/classes for differences and only a fraction reads the N&N especially if there are tutorials out in the internet that are decades old. But a (deprecation, resource-leak, what ever) warning is often a good way to make people aware of a better alternative. |
you would need to
but i don't think its worth the effort, as SWT already offers checks to find non disposed Resources - but only few people care about. |
@tobiasmelcher would you be interested in implementing a solution like this? |
which takes care of creation and disposal of the GC object Fixes: eclipse-platform#955
which takes care of creation and disposal of the GC object Fixes: eclipse-platform#955
Thank you @tobiasmelcher for providing #1063. While reviewing your PR and looking at the applications of the new method I noticed two major drawbacks of the
To address the first point we already have On the other hand, when using an auto-closable in a try-with-resources block, the handling of checked and unchecked exceptions can just be left unchanged. And also assigning one, multiple or no variable can be done as it is done now. Many use-cases of GC in the Eclipse SDK even already use a try-finally block to ensure the GC is disposed. But at the same time I agree that implementing To make a long story short, to get the best of both worlds I propose to created a
Users can then change their code from
to
without being disturbed at any existing call to Instead of
What do you think? If in the future we have the impression that GC#create() is used enough, we could then still deprecate its public constructor and/or just implement |
There is also |
I actually looked for it, but somehow oversaw it.
Yes that would be an option, but while both solutions of their pro's and con's and are similar complicated depending on the exact use-case, the Runable+Callable approach still is inconvenient if multiple variables should be assigned, which is already necessary in the applications in the PR. |
Please take a look at 37519e2 . What do you think? I like your proposal. Is this change downward compatible? |
Thanks, added my review in the PR. And yes since we only add a new method and class that change is fully compatible. |
which takes care of creation and disposal of the GC object Fixes: eclipse-platform#955
I suggest to implement the java.lang.AutoCloseable interface in org.eclipse.swt.graphics.GC so that the try-with-resources Java feature can be used.
Here a concrete example in org.eclipse.jface.dialogs.Dialog where the GC class is used.
gc.dispose() is not called when JFaceResources.getDialogFont() throws an exception.
Wouldn't it be much nicer if the try-with-resource approach would be used here:
Did you already discuss this topic? Are there any drawbacks of implementing the AutoCloseable interface in GC?
The text was updated successfully, but these errors were encountered: