Skip to content
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

Specialize Function1 and Function2 for AnyRef #5267

Closed
scabug opened this issue Dec 4, 2011 · 7 comments
Closed

Specialize Function1 and Function2 for AnyRef #5267

scabug opened this issue Dec 4, 2011 · 7 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Dec 4, 2011

Scala 2.9 introduced partial specialization (add AnyRef to the list of @specialized targets). However this wasn't employed in any of the handful of specialized classes in the standard library.

I think this would be a valuable addition to Function1. Without this, I recently resorted to:

abstract class Function1ADouble[A] extends Function1[A, Double] {
  def apply(a: A): Double
}

def mapSum[A](as: Iterable[A])(f: Function1ADouble[A]): Double = {
    var sum = 0d
    val iterator = as.iterator
    while (iterator.hasNext) {
      sum += f.apply(iterator.next())
    }
    sum
}

}

Function2 might also be a candidate, although it will add more weight.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Dec 4, 2011

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Feb 15, 2012

@paulp said:
bb23d766bc

@scabug scabug closed this Feb 15, 2012
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Feb 15, 2012

@ijuma said:
Yay! :)

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Oct 10, 2014

Marcin Kielar (zorba128) said:
Hi

Just wanted to ask what is the status of partial Function1 specialization - this ticket seems closed/resolved, while Function1 in scala 2.11.2 doesn't seem to be AnyRef specialized...

regards,
Marcin

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Oct 10, 2014

@retronym said (edited on Oct 10, 2014 7:46:54 AM UTC):
Unfortunately AnyRef specialization had to be removed from Functions in scala/scala@cc3bada. Discussion of the problems it caused is here: https://groups.google.com/d/msg/scala-internals/5P5TS9ZWe_w/39S8q1WJYF8J.

We don't have plans to revisit this in forseeable future.

What we do have, however, is experimental support for SAMs in Scala 2.11.

scala> trait IntFunction[A] { def apply(i: A): Int }
defined trait IntFunction

scala> def sum[T](as: Traversable[T])(f: IntFunction[T]) = { var sum = 0; as foreach (a => sum += f(a)); sum }
sum: [T](as: Traversable[T])(f: IntFunction[T])Int

scala> sum(List("a", "bc"))(_.length)
res2: Int = 3

No boxing is needed here.

@texasbruce

This comment has been minimized.

Copy link

@texasbruce texasbruce commented Jun 13, 2019

@retronym Issue was closed in 2014, but now that we have Java 8 java.util.function package with all predefined SAM interfaces, can we look into the possibility of translating "partially specialized" FunctionXs into the SAM type objects? E.g.

case class Foo(i: Int)
def getFooField(foo: Foo, f: Foo=>Int): Int = f(foo)

Instead of compiling into

getFooField:(LFoo;Lscala/Function1;)Ljava.lang.Integer    //Int is boxed when invokevirtual

can the compiler compile it into

getFooField:(LFoo;Ljava.util.function.ToIntFunction):I    //No boxing for returned Int

We could use self-defined SAM, or even Java SAM types, but Scala should have its own set, or being able to translate it under the hood.

@retronym

This comment has been minimized.

Copy link
Member

@retronym retronym commented Jun 16, 2019

@texasbruce The compiler logic that would decide to use such an interface is the buggy part of Scala's specialization transform. We don't see a path forward to getting it working as its one of the trickiest areas of the compiler even before dealing with partial specialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.