元组函数的 Scala 返回类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2743866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala return type for tuple-functions
提问by Felix
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).
这将工作正常,但现在我想告诉编译器我期望从函数返回什么,而不是使用内置类型推断(毕竟,我不知道 a(1,"hello","world")是什么)。
回答by oxbow_lakes
def foo : (Int, String, String) = (1, "Hello", "World")
The compiler will interpret the type (Int, String, String)as a Tuple3[Int, String, String]
编译器会将类型解释(Int, String, String)为Tuple3[Int, String, String]
回答by WillD
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")

