从 Java 调用 Scala "val function" 给出错误

Call a Scala "val function" from Java gives error

我有一个 scala val 函数如下:

  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()
  }

当我从 Java 调用此函数时, 我收到以下错误:

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

Java 调用 scala val 函数的代码:

Long minTimestampOfGranularity = getTimestampEpochMillis(year, 1, 1, 0, 0, 0, 0);

尝试

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

并注意 getTimestampEpochMillis() 中的括号 ()。使用 javap 检查生成的 class 我们得到

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();

在我们看到 getTimestampEpochMillis() returns scala.Function7 的地方,所以我们必须先调用 getTimestampEpochMillis(),然后才能 apply 参数。