Skip to content

Added function converter capability. #39

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

Closed
wants to merge 2 commits into from

Conversation

Ichoran
Copy link
Contributor

@Ichoran Ichoran commented Jun 2, 2015

Based on initial reflection work from Jason Zaugg.

If you have something from java.util.functions, you should be able to

import scala.compat.java8.FunctionConverters._

and then use .asScala to convert to the corresponding Scala function.

If you have a Scala function, use .asJava to convert to the best-matching item from java.util.functions.

The code is created with a code generator that needs to run in Scala 2.11+, so build.sbt was reworked to be multi-module (the sub-module is the code generator).

@retronym
Copy link
Member

retronym commented Jun 5, 2015

@Ichoran The build failure is due to the lack of quasiquotes in Scala 2.10. I'm going to drop 2.10 support at this point in a separate PR, so we can disregard those.

@retronym
Copy link
Member

retronym commented Jun 5, 2015

I'd like to the approach to implicit priorities documented and tested.

Here's one example that remains ambiguous:

scala> ((x: Any) => "").asJava
<console>:45: error: type mismatch;
 found   : Any => String
 required: ?{def asJava: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method wrap_asJava_IntFunction in trait Priority1FunctionConverters of type [R](scalaFn: Int => R)scala.compat.java8.WrappedAsJava[java.util.function.IntFunction[R]]
 and method wrap_asJava_LongFunction in trait Priority1FunctionConverters of type [R](scalaFn: Long => R)scala.compat.java8.WrappedAsJava[java.util.function.LongFunction[R]]
 are possible conversion functions from Any => String to ?{def asJava: ?}
              ((x: Any) => "").asJava
                        ^
<console>:45: error: value asJava is not a member of Any => String
              ((x: Any) => "").asJava
                               ^

@Ichoran
Copy link
Contributor Author

Ichoran commented Jun 5, 2015

@retronym - Hmm, I hadn't anticipated that (but should have). It's breaking the most-specific-type part of the computation. But then because of the inversion of specificity due to covariance of function arguments, I wonder whether I need the prioritization at all? I'll play around with it this weekend. I agree I need tests and docs. I don't recall why I pushed this before writing them--maybe I wanted feedback? (Thanks!)

@Ichoran
Copy link
Contributor Author

Ichoran commented Jun 5, 2015

Hmm, I hadn't anticipated that (but should have). Contravariance of function arguments makes things trickier than usual. I might need to add some type identity computations to get it to work. I'll work on it this weekend. (Also--I know I need tests and docs. I think I might have pushed this because I wanted feedback?--I don't recall. Anyway, thanks!)

Ichoran added 2 commits July 9, 2015 17:55
…from Jason Zaugg.

If you have something from java.util.functions, you should be able to

```
import scala.compat.java8.FunctionConverters._
```

and then use `.asScala` to convert to the corresponding Scala function.

If you have a Scala function, use `.asJava` to convert to the best-matching item from java.util.functions.
If you want a function that isn't the best match, use the corresponding `asJavaInterfaceName` method, e.g.

```
asJavaUnaryOperator((i: Int) => i+1)   // Best match would be IntUnaryOperator

The code is created with a code generator that needs to run in Scala 2.11+,
but SBT runs 2.10 by default.  Thus, build.sbtt was reworked to be
multi-module (the sub-module is the code generator).
These were written manually to avoid making the same assumptions in code
generation for the converters and the tests, and thereby missing important
bugs.  As a downside, though, they do not have complete code coverage.  This
should not matter given how the code generator works, but it is a weak point.
@Ichoran Ichoran force-pushed the sam-function-compat branch from d180fc5 to a685a15 Compare July 10, 2015 01:01
@Ichoran
Copy link
Contributor Author

Ichoran commented Jul 10, 2015

@retronym - I did a fairly extensive reworking of the PR, and added tests. I think this is now a decent candidate for inclusion (after appropriate review and modification).

@retronym retronym mentioned this pull request Jul 28, 2015
@retronym
Copy link
Member

Continued in #48

@retronym retronym closed this Jul 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants