How to skip optional parameters in Scala?

前端 未结 3 1596
[愿得一人]
[愿得一人] 2021-02-19 03:30

Given the following function with optional parameters:

def foo(a:Int = 1, b:Int = 2, c:Int = 3) ...

I want to keep the default value of a

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 03:47

    You can create another function, with one of the parameters applied.

    def long(a: Int = 1, b: Int = 2, c: Int = 3) = a + b + c
    
    def short(x: Int, y: Int) = long(b = x, c = y)
    
    val s = short(10, 20) // s: Int = 31
    

提交回复
热议问题