What are all the uses of an underscore in Scala?

后端 未结 7 1166
南笙
南笙 2020-11-21 07:24

I\'ve taken a look at the list of surveys taken on scala-lang.org and noticed a curious question: \"Can you name all the uses of “_”?\". Can you? If yes, please do so here.

相关标签:
7条回答
  • 2020-11-21 07:53

    Besides the usages that JAiro mentioned, I like this one:

    def getConnectionProps = {
        ( Config.getHost, Config.getPort, Config.getSommElse, Config.getSommElsePartTwo )
    }
    

    If someone needs all connection properties, he can do:

    val ( host, port, sommEsle, someElsePartTwo ) = getConnectionProps
    

    If you need just a host and a port, you can do:

    val ( host, port, _, _ ) = getConnectionProps
    
    0 讨论(0)
提交回复
热议问题