Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining a scala function1 for use by java has wrong types

Tags:

generics

scala

The problem can be summarised as follows:

I have a class that defines several methods to be called from java. one of them has a signature like:

import java.lang.{Double => JDouble}

def compute(x: Double, fun: Function1[Double,Double]) = ???

or

def compute(x: Double, fun: Double => Double) = ???

when i use javap on any of them, it looks like this:

Double compute(double, scala.Function1<java.lang.Object, java.lang.Object>)

if I use the following aliasing

 import java.lang.{Double => JDouble}
 def compute(x: Double, fun: JDouble => JDouble) = 

it looks correct with javap

public Double compute( double, scala.Function1<java.lang.Double, java.lang.Double>);

Why is this?

like image 654
fracca Avatar asked Mar 16 '26 23:03

fracca


1 Answers

In Scala Double extends AnyVal. When you javap this function the nearest super class finds and in case AnyVal the nearest Java super class is Object.

like image 112
Denis Avatar answered Mar 18 '26 20:03

Denis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!