简体   繁体   English

从Java调用Scala“ val函数”会出错

[英]Call a Scala “val function” from Java gives error

I have a scala val function as follows : 我有一个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()
  }

When I am calling this function from Java, I get the below error: 当我从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 code invoking scala val-function: 调用scala val函数的Java代码:

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

Try 尝试

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

and note the parenthesis () in getTimestampEpochMillis() . 并注意括号()getTimestampEpochMillis() Examining the generated class using javap we get 使用javap检查生成的类,我们得到

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. 我们在其中看到getTimestampEpochMillis()返回scala.Function7 ,因此在apply参数之前,我们首先必须调用getTimestampEpochMillis()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM