- 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.
推薦閱讀
- 測試驅動開發:入門、實戰與進階
- 程序設計與實踐(VB.NET)
- Mastering ServiceStack
- Rust編程:入門、實戰與進階
- 高效微控制器C語言編程
- Learning ArcGIS Pro 2
- C#程序設計(慕課版)
- Hands-On Swift 5 Microservices Development
- Mastering Drupal 8 Views
- Apache Spark 2.x for Java Developers
- C#應用程序設計教程
- HTML 5與CSS 3權威指南(第3版·上冊)
- Java Web從入門到精通(第3版)
- Python:Deeper Insights into Machine Learning
- 運維前線:一線運維專家的運維方法、技巧與實踐