How to pass a tuple argument the best way?

后端 未结 3 798
猫巷女王i
猫巷女王i 2021-02-01 02:12

How to pass a tuple argument the best way ?

Example:

def foo(...): (Int, Int) = ...

def bar(a: Int, b: Int) = ...

Now I would like to

3条回答
  •  不要未来只要你来
    2021-02-01 02:50

    Using tupled, as @Tomasz mentions, is a good approach.

    You could also extract the tuple returned from foo during assignment:

    val (x, y) = foo(5)
    bar(x, y)
    

    This has the benefit of cleaner code (no _1 and _2), and lets you assign descriptive names for x and y, making your code easier to read.

提交回复
热议问题