I like James Iry's suggestion of making FunctionN a value class that wraps a functional interface, but he also pointed out that that's not a source compatible change (FunctionN is not final). It would not require anything beyond Java 6, but would require breaking our compatibility policy (in a minor way) if we want to do this in 2.11.
--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Function1 couldn't be final, it would be a SAM interface.What would change is the location of compose and andThe - they would be moved to an implicit conversion (using a value class to avoid boxing)The source incompatibility would break anything overriding compose or andThen.
What would change is the location of compose and andThe - they would be moved to an implicit conversion (using a value class to avoid boxing)The source incompatibility would break anything overriding compose or andThen.
Hi,Wanted to summarize the current thinking on the roadmap to supporting Java 8 lambdas.The JSR 335 spec seems to allow default methods in interfaces, so, for 2.12, we should plan to compile FunctionN to a "functional interface" (an interface with one abstract method, and default methods for the concrete ones).
What are the steps we need to take in 2.11 to make this possible?Also, what can we do in 2.11 (assuming Java 6) to allow our higher-order methods to be called from Java 8 using their closure syntax?There are various proposals:
- The most conservative approach: for each higher-order method (a method that takes a FunctionN argument), synthesize an overload that's a Java 8-style higher-order method. It boxes its argument of "functional interface type" to the Scala-style FunctionN. By introducing that functional interface as a superclass to FunctionN, we don't have to do anything for returning closures.
- Disadvantages: the cost of boxing, additional overloads result in more bytecode, potential confusion (from Java; should be able to mark them synthetic so they are invisible to Scala)
- Advantage: works on Java 6, and is source compatible, so that we can do it in 2.11
- I like James Iry's suggestion of making FunctionN a value class that wraps a functional interface, but he also pointed out that that's not a source compatible change (FunctionN is not final). It would not require anything beyond Java 6, but would require breaking our compatibility policy (in a minor way) if we want to do this in 2.11.
- Compile FunctionN to a real functional interface with default methods (as defined by JSR 335). I think this is the ideal outcome for Scala 2.12 on Java 8. We should deprecate as necessary in 2.11 to make this transition possible.
cheers,adriaan
--
Specialization makes everything a bit more complicated/convoluted (as usual), but I don't see any truly new challenges arise from it.In terms of the first proposal, specialization simply produces more methods to overload with a Java variant.
Am I missing something?
On Wed, Mar 27, 2013 at 9:17 PM, Adriaan Moors <adriaa...@typesafe.com> wrote:
Specialization makes everything a bit more complicated/convoluted (as usual), but I don't see any truly new challenges arise from it.In terms of the first proposal, specialization simply produces more methods to overload with a Java variant.
Am I missing something?Ah, so the FI will only have the apply?trait ƒ[-T, +R] {def apply(param: T): R}But how will you go from ƒ[T,R] => (T => R) while retaining specialization (i.e. avoiding boxing)?Am I missing something?
Ah, so the FI will only have the apply?
trait ƒ[-T, +R] {def apply(param: T): R}But how will you go from ƒ[T,R] => (T => R) while retaining specialization (i.e. avoiding boxing)?
Option zero would be to do _nothing with Scala at all_ but add twenty-two generator methods written in Java that take a SAM and produce a Scala function. If you use Java, it's your job to wrap your higher-order method call in that generator. The java wrapper could be packaged in scala-interop.jar or somesuch and shipped with the distribution.
Upside: Function1 can be used as a Java SAM (of course we'll still need to deal with compose/andThen)
public default <V > Function<T, V> compose(Function<? super R, ? extends V> after) {
return (T t) -> after.apply(apply(t));
}
def compose[A](g: A => T1): A => R = { x => apply(g(x)) }
Precisely.
Then, from Java you would use it this way:scala.collection.immutable.List<Int> xs = ...;xs.map(asScala((Integer x) -> x+1)));
--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Another concern is that Java's type inference will have to work harder ...
Map<String, List<String>> map =
new
LinkedHashMap<>();
map.put(
"UK"
, asList(
"Bermingham"
,
"Bradford"
,
"Liverpool"
));
map.put(
"USA"
, asList(
"NYC"
,
"New Jersey"
,
"Boston"
,
"Buffalo"
));
FlatMapper<Entry<String, List<String>>,String> flattener;
flattener = (entry,consumer) -> { entry.getValue().forEach(consumer); };
List<String> cities = map.entrySet()
.stream()
.flatMap( flattener )
.collect(toList());
Ideally the lines 5, 6 should be defined as inline arguments of
flatMap
, but the compiler cannot properly infer the types of the expression precisely for the overloading problems that I described above. Given the amount of type information that must be provided, I felt that it was best to define it in another line as I did in #5 and #6. It looks awful, I know. Clearly, we are in desperate need of an API that offers operations to deal with maps or tuples.