Scala return type for tuple

I want to make a scala function which returns a scala tuple.

I can do a function like this:

def foo = (1,"hello","world")

and this will work fine, but now I want to tell the compiler what I expect to be returned from the function instead of using the built in type inference (after all, I have no idea what a (1,"hello","world") is).


def foo : (Int, String, String) = (1, "Hello", "World")

编译器会将类型(Int, String, String)Tuple3[Int, String, String]


另外,如果您厌倦了写入(Int,String,String),则可以创建一个类型别名

type HelloWorld = (Int,String,String)
...

def foo : HelloWorld = (1, "Hello", "World")
/// and even this is you want to make it more OOish
def bar : HelloWorld = HelloWorld(1, "Hello", "World")
链接地址: http://www.djcxy.com/p/38012.html

上一篇: 类型不匹配在斯卡拉理解

下一篇: Scala返回类型为元组