开发者

Scala return type for tuple-functions

开发者 https://www.devze.com 2022-12-28 13:17 出处:网络
I want to make a scala function which returns a scala tuple. I can do a function like this: def foo = (1,\"hello\",\"world\")

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")

The compiler will interpret the type (Int, String, String) as a Tuple3[Int, String, String]


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

精彩评论

暂无评论...
验证码 换一张
取 消