Fel*_*lix 42 types scala tuples return function
我想创建一个scala函数,它返回一个scala元组.
我可以做这样的功能:
def foo = (1,"hello","world")
Run Code Online (Sandbox Code Playgroud)
这将工作正常,但现在我想告诉编译器我期望从函数返回什么而不是使用内置类型推断(毕竟,我不知道是什么(1,"hello","world")).
oxb*_*kes 69
def foo : (Int, String, String) = (1, "Hello", "World")
Run Code Online (Sandbox Code Playgroud)
编译器会将类型解释(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")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36186 次 |
| 最近记录: |