简体   繁体   English

Scala返回类型为元组函数

[英]Scala return type for tuple-functions

I want to make a scala function which returns a scala tuple. 我想创建一个scala函数,它返回一个scala元组。

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). 这将工作正常,但现在我想告诉编译器我希望从函数返回什么而不是使用内置类型推断(毕竟,我不知道是什么(1,"hello","world")是)。

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

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

Also, you can create a type alias if you get tired of writing (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")

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

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