- 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.
推薦閱讀
- TypeScript Blueprints
- Java入門(mén)經(jīng)典(第6版)
- Vue.js 2 and Bootstrap 4 Web Development
- Learning ArcGIS Pro 2
- Python Tools for Visual Studio
- Mastering OpenCV 4
- Securing WebLogic Server 12c
- Spring Boot進(jìn)階:原理、實(shí)戰(zhàn)與面試題分析
- RabbitMQ Essentials
- CoffeeScript Application Development Cookbook
- Java程序員面試筆試寶典(第2版)
- Node.js 12實(shí)戰(zhàn)
- Advanced UFT 12 for Test Engineers Cookbook
- Instant jQuery Boilerplate for Plugins
- Qt 4開(kāi)發(fā)實(shí)踐