scala语言示例_标有示例的Scala关键字
scala語言示例
Scala | 任一關鍵字 (Scala | Either Keyword)
Either is a container similar to the option which has two values, they are referred to as children. The left and right children are named as the right child and left child.
這是一個類似于選項的容器,該容器具有兩個值,它們被稱為子級。 左邊的孩子和右邊的孩子分別命名為右邊的孩子和左邊的孩子 。
The left child is similar to None class which is used when there can be an error returned.
左子級類似于None類,該類可以在返回錯誤時使用。
The right child is similar to Some class which is used when a vale is to be returned i.e. for the successful execution of code.
正確的子級類似于Some類,該類將在返回谷時即成功執行代碼時使用。
Syntax:
句法:
Either [left, right]Both left and right are data types of the returned values which can be used to define the results when there are error case or valid case.
左和右均為返回值的數據類型,當出現錯誤情況或有效情況時,可用于定義結果。
理解任一關鍵字工作方式的示例 (Example to understand the working of Either Keyword)
object MyObject {// function defintiondef isEven(number : Int ): Either[String, String] = {if(number%2 == 0){Right(number + " is even.")}elseLeft(number + " is not even.")}// main codedef main(args: Array[String]) {println(isEven(4))println(isEven(95))} }Output
輸出量
Right(4 is even.) Left(95 is not even.)翻譯自: https://www.includehelp.com/scala/either-keyword.aspx
scala語言示例
總結
以上是生活随笔為你收集整理的scala语言示例_标有示例的Scala关键字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: macos可以升级到指定版本吗_承装承修
- 下一篇: 带有示例的Python列表reverse