Call a Scala "val function" from Java gives error

Remis Haroon :

I have a scala val function as follows :

  val getTimestampEpochMillis = (year:Int, month:Int, day:Int, hour:Int, quarterHour:Int, minute:Int, seconds:Int) => {
    var tMinutes = minute
    var tSeconds = seconds
    if(minute == 0){
      if(quarterHour == 1){
        tMinutes = 22
        tSeconds = 30
      }else if(quarterHour == 2){
        tMinutes = 37
        tSeconds = 30
      }else if(quarterHour == 3){
        tMinutes = 52
        tSeconds = 30
      }else if(quarterHour == 0){
        tMinutes = 7
        tSeconds = 30
      }
    }

    val localDateTime = LocalDateTime.of(year, month, day, hour, tMinutes, tSeconds)
    localDateTime.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()
  }

When I am calling this function from Java, I get the below error:

[ERROR]   required: no arguments                                          
[ERROR]   found: int,int,int,int,int,int,int                              
[ERROR]   reason: actual and formal argument lists differ in length    

Java code invoking scala val-function:

Long minTimestampOfGranularity = getTimestampEpochMillis(year, 1, 1, 0, 0, 0, 0);
Mario Galic :

Try

getTimestampEpochMillis().apply(year, 1, 1, 0, 0, 0, 0);

and note the parenthesis () in getTimestampEpochMillis(). Examining the generated class using javap we get

public scala.Function7<java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, scala.runtime.BoxedUnit> getTimestampEpochMillis();

where we see getTimestampEpochMillis() returns scala.Function7, so we first have to call getTimestampEpochMillis() before we can apply the arguments.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=127876&siteId=1