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
Feature/direct lambda assignment for event handlers #196
Feature/direct lambda assignment for event handlers #196
Conversation
The type bound specified in Java is `? super TransformChangedEvent` which in Scala would be `_ >: TransformChangedEvent`
Also, corrected typos for assignment methods of the event handlers
After thinking about it for a bit, I think it would be better if the function types for the assignment method took in ScalaFX event types instead of JavaFX event types. It would allow for more natural access patterns of the event's methods when writing code. On that note, are there implicit conversions from JavaFX types to ScalaFX types? If not, doing the conversion on a per-method basis might get a little hairy. |
Well, I found the answer to my question: yes, there are implicit conversions from JavaFX types to ScalaFX types, at least for the events (which is what really matters here). The only real issue is that some method signatures become a fair bit more complicated. To handle the type bounds specified in JavaFX, I currently do this: onDragDone_=[T >: jfxsi.DragEvent <: jfxe.Event](handler: T => Unit) To use ScalaFX types instead, the signature turns into: onDragDone_=[T >: DragEvent <: Event, U >: jfxsi.DragEvent <: jfxe.Event](handler: T => Unit)(implicit jfx2sfx: U => T) Most of it is irrelevant if you just let the type inferencer do its work, but it might get a little complicated to read. It'll probably take about a day or two to do all the conversion. |
`handle` now breaks as it returns an `EventHandler[Nothing]` which type errors against the required type of the `EventHandler[]`
The interaction between this implicit conversion and the overloaded lambda assignment methods cause the compiler to be unable to infer the type of any lambda that is to be assigned, because there are now 2 methods with the same signature, and the compiler has not enough information in order to pick either one of them.
I am not able to build the code. Getting a couple of compilation errors, for instance:
Please make sure that the code compiles and all the test pass before submitting a PR. The Travis CI setup was fixed, so that should be able to run tests too (you will need to mere it from I looked only briefly though the code and had some concerns. For instance:
I could not test/review in details since the code was not building. |
The KeyFrame code strongly relies on being able to receive an EventHandler rather than an actual lambda. This means that an implicit conversion from a lambda into an EventHandler is absolutely necessary, and the lack of such a conversion would result in a lot of changes across the codebase. As a fix, I have restored the original implicit conversion. The caveat now is that lambda assignment must type annotate the lambda parameters, which is what this branch set out to do in the first place. For now, lambda assignment is introduced in a backward compatible manner, with the only minor improvement being that no implicit conversion is required at the user level to assign lambdas.
Regarding your point that existing event handlers can return any type. The function signature of the JavaFX event handler method Also, I'm not sure what you mean by the With regards to incompatible changes, the |
Is commit 7728617 supposed to be part of this PR? It seems to require that lambdas have they argument types provided. Can you clarify what benefits for ScalaFX this PR brings in its latest form? |
7728617 is part of the PR, as it fixes tests that don't compile. The reason those lambdas have type annotations is due to the implicits from The main benefit to ScalaFX is being able to use ScalaFX in a more Scala-like manner without resorting to implicits. As much as I love implicits, having too many implicits in a project increases the likelihood of implicit collision (with other implicits or with something non-implicit), and debugging errors caused by implicits isn't easy, because the error messages aren't always helpful, and it can be difficult to find the source of the error. |
…do not build should not be submitted. Added factory methods for `KeyFrame` to go around two issues: 1) implicit conversion from a lambda to an event handler was removed in PR to avoid compilation issues. 2) In Scala methods with default parameters cannot be overloaded, so each variant corresponding to JavaFX API is reimplemented.
In you very first comment for this PR you state "This fixes #194.". After commit commit 7728617 this is no longer the case. You no longer can write:
You need to write as before the PR, providing argument type:
I did implement alternative workaround for KeyFrame factory methods that is using PR without commit 7728617. It is available here [https://github.com/jpsacha/scalafx/tree/feature/PR196_Event_Lambdas]. IT let you use notation like:
Though that key problem is that while it works well with current compilers, it does not work when SAMs are enabled ("-Xexperimental"). You have to again specify argument types. I will try to extract correction to types tat are part of the PR like correction from
to
but overloaded assignment operators that add explicit lambdas should not be merged as they do not improve notation as requested by #194 and do not work as well with SAMs as the current code. By the way, a version of ProScalaFX examples modified to use simpler event handler using SAMs is here [https://github.com/scalafx/ProScalaFX/tree/SAM]. It used regular ScalaFX code from the |
* Scene's `onMouseDrag` setters had incorrect type parameters, `jfxsi.MouseEvent` instead `jfxsi.MouseDragEvent` - Issue #220. * Correct some type constraints in methods using jfxe.EventHandler arguments - Issue #221. * Correct some setter that were defined with `_(` instead of `_=(` - Issue ##222. * Typos in documentation.
This no longer needed in Scala 2.12 and newer. Statements like cellFactory = { tableColumn => ... } are handled out of the box. |
This fixes #194. Also, along the way, I've correct some typos related to the lambda assignment.
This, however, seems to break anything using the
handle
function.handle
seems to be returning a lot ofEventHandler[Nothing]
which type errors when applied against anything that expects anEventHandler[(not Nothing)]
or a lambda.Example: