- Learn Scala Programming
- Slava Schmidt
- 106字
- 2021-06-10 19:35:39
Optional parsing for string literals
In Scala 2.13, StringOps has been extended with methods that return Option for string-literals parsing. Supported types include all numeric types and Boolean.
The new methods can greatly simplify the processing of user-provided data without the need to wrap the calls with the exception-handling, as shown in the following example:
scala> "10".toIntOption
res3: Option[Int] = Some(10)
scala> "TrUe".toBooleanOption
res4: Option[Boolean] = Some(true)
scala> val bool = "Not True"
bool: String = Not True
scala> bool.toBooleanOption
res5: Option[Boolean] = None
The optional Boolean parsing ignores the case of the argument the same way the exception-throwing toBoolean method does.
推薦閱讀
- AngularJS入門與進階
- 騰訊iOS測試實踐
- Python自動化運維快速入門(第2版)
- Java Web基礎與實例教程(第2版·微課版)
- HTML5游戲開發案例教程
- Internet of Things with Intel Galileo
- Mastering KnockoutJS
- Unity Game Development Scripting
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- Python深度學習原理、算法與案例
- 區塊鏈技術進階與實戰(第2版)
- 軟件測試分析與實踐
- Unity 3D UI Essentials
- Python深度學習:基于PyTorch
- Test-Driven Java Development(Second Edition)