A library that makes some Scala 2.13 APIs available on Scala 2.11 and 2.12, facilitating cross-building Scala 2.13 and 3.0 code on older versions
—
Safe string parsing methods that return Option types instead of throwing exceptions, providing a more functional approach to string-to-number conversions.
implicit class StringOps(s: String) {
def toBooleanOption: Option[Boolean]
def toByteOption: Option[Byte]
def toShortOption: Option[Short]
def toIntOption: Option[Int]
def toLongOption: Option[Long]
def toFloatOption: Option[Float]
def toDoubleOption: Option[Double]
}All methods return Option types and handle parsing failures gracefully by returning None instead of throwing exceptions.
def toBooleanOption: Option[Boolean]Attempts to parse the string as a Boolean.
Returns:
Some(true) if the string is "true" (case insensitive)Some(false) if the string is "false" (case insensitive)None for any other valueThrows:
NullPointerException if the string is nullUsage:
"true".toBooleanOption // Some(true)
"TRUE".toBooleanOption // Some(true)
"false".toBooleanOption // Some(false)
"False".toBooleanOption // Some(false)
"yes".toBooleanOption // None
"1".toBooleanOption // Nonedef toByteOption: Option[Byte]Attempts to parse the string as a Byte (range: -128 to 127).
Returns:
Some(value) if the string contains a valid byte valueNone if parsing fails or the value is out of rangeThrows:
NullPointerException if the string is nullUsage:
"42".toByteOption // Some(42)
"127".toByteOption // Some(127)
"-128".toByteOption // Some(-128)
"128".toByteOption // None (out of range)
"abc".toByteOption // Nonedef toShortOption: Option[Short]Attempts to parse the string as a Short (range: -32,768 to 32,767).
Returns:
Some(value) if the string contains a valid short valueNone if parsing fails or the value is out of rangeThrows:
NullPointerException if the string is nullUsage:
"1000".toShortOption // Some(1000)
"32767".toShortOption // Some(32767)
"-32768".toShortOption // Some(-32768)
"40000".toShortOption // None (out of range)
"text".toShortOption // Nonedef toIntOption: Option[Int]Attempts to parse the string as an Int.
Returns:
Some(value) if the string contains a valid integer valueNone if parsing failsThrows:
NullPointerException if the string is nullUsage:
"42".toIntOption // Some(42)
"-123".toIntOption // Some(-123)
"2147483647".toIntOption // Some(2147483647)
"3.14".toIntOption // None
"hello".toIntOption // Nonedef toLongOption: Option[Long]Attempts to parse the string as a Long.
Returns:
Some(value) if the string contains a valid long valueNone if parsing failsThrows:
NullPointerException if the string is nullUsage:
"1234567890".toLongOption // Some(1234567890L)
"9223372036854775807".toLongOption // Some(9223372036854775807L)
"-9223372036854775808".toLongOption // Some(-9223372036854775808L)
"not_a_number".toLongOption // Nonedef toFloatOption: Option[Float]Attempts to parse the string as a Float.
Returns:
Some(value) if the string is a parsable FloatNone if parsing failsThrows:
NullPointerException if the string is nullUsage:
"3.14".toFloatOption // Some(3.14f)
"-42.5".toFloatOption // Some(-42.5f)
"1e10".toFloatOption // Some(1.0E10f)
"Infinity".toFloatOption // Some(Float.PositiveInfinity)
"invalid".toFloatOption // Nonedef toDoubleOption: Option[Double]Attempts to parse the string as a Double.
Returns:
Some(value) if the string is a parsable DoubleNone if parsing failsThrows:
NullPointerException if the string is nullUsage:
"3.14159".toDoubleOption // Some(3.14159)
"-42.5".toDoubleOption // Some(-42.5)
"1.23e-4".toDoubleOption // Some(1.23E-4)
"NaN".toDoubleOption // Some(Double.NaN)
"invalid".toDoubleOption // Noneimport scala.collection.compat._
def parseUserInput(input: String): Either[String, Double] = {
input.toDoubleOption match {
case Some(value) => Right(value)
case None => Left(s"Invalid number: $input")
}
}
// Safe configuration parsing
def parseConfig(config: Map[String, String]): Option[AppSettings] = {
for {
port <- config.get("port").flatMap(_.toIntOption)
timeout <- config.get("timeout").flatMap(_.toLongOption)
enabled <- config.get("enabled").flatMap(_.toBooleanOption)
} yield AppSettings(port, timeout, enabled)
}
// Safe arithmetic
def divide(a: String, b: String): Option[Double] = {
for {
num1 <- a.toDoubleOption
num2 <- b.toDoubleOption
if num2 != 0.0
} yield num1 / num2
}Before (throws exceptions):
try {
val value = "42".toInt
// handle value
} catch {
case _: NumberFormatException => // handle error
}After (with Option):
"42".toIntOption match {
case Some(value) => // handle value
case None => // handle error
}NullPointerException for null inputInstall with Tessl CLI
npx tessl i tessl/maven-org-scala-lang-modules--scala-collection-compat