CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scala-lang-modules--scala-collection-compat

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

Pending
Overview
Eval results
Files

string-parsing.mddocs/

String Parsing

Safe string parsing methods that return Option types instead of throwing exceptions, providing a more functional approach to string-to-number conversions.

StringOps Extension Methods

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.

Method Details

toBooleanOption

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 value

Throws:

  • NullPointerException if the string is null

Usage:

"true".toBooleanOption    // Some(true)
"TRUE".toBooleanOption    // Some(true)
"false".toBooleanOption   // Some(false)
"False".toBooleanOption   // Some(false)
"yes".toBooleanOption     // None
"1".toBooleanOption       // None

toByteOption

def 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 value
  • None if parsing fails or the value is out of range

Throws:

  • NullPointerException if the string is null

Usage:

"42".toByteOption      // Some(42)
"127".toByteOption     // Some(127)
"-128".toByteOption    // Some(-128)
"128".toByteOption     // None (out of range)
"abc".toByteOption     // None

toShortOption

def 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 value
  • None if parsing fails or the value is out of range

Throws:

  • NullPointerException if the string is null

Usage:

"1000".toShortOption     // Some(1000)
"32767".toShortOption    // Some(32767)
"-32768".toShortOption   // Some(-32768)
"40000".toShortOption    // None (out of range)
"text".toShortOption     // None

toIntOption

def toIntOption: Option[Int]

Attempts to parse the string as an Int.

Returns:

  • Some(value) if the string contains a valid integer value
  • None if parsing fails

Throws:

  • NullPointerException if the string is null

Usage:

"42".toIntOption        // Some(42)
"-123".toIntOption      // Some(-123)
"2147483647".toIntOption // Some(2147483647)
"3.14".toIntOption      // None
"hello".toIntOption     // None

toLongOption

def toLongOption: Option[Long]

Attempts to parse the string as a Long.

Returns:

  • Some(value) if the string contains a valid long value
  • None if parsing fails

Throws:

  • NullPointerException if the string is null

Usage:

"1234567890".toLongOption           // Some(1234567890L)
"9223372036854775807".toLongOption  // Some(9223372036854775807L)
"-9223372036854775808".toLongOption // Some(-9223372036854775808L)
"not_a_number".toLongOption         // None

toFloatOption

def toFloatOption: Option[Float]

Attempts to parse the string as a Float.

Returns:

  • Some(value) if the string is a parsable Float
  • None if parsing fails

Throws:

  • NullPointerException if the string is null

Usage:

"3.14".toFloatOption     // Some(3.14f)
"-42.5".toFloatOption    // Some(-42.5f)
"1e10".toFloatOption     // Some(1.0E10f)
"Infinity".toFloatOption // Some(Float.PositiveInfinity)
"invalid".toFloatOption  // None

toDoubleOption

def toDoubleOption: Option[Double]

Attempts to parse the string as a Double.

Returns:

  • Some(value) if the string is a parsable Double
  • None if parsing fails

Throws:

  • NullPointerException if the string is null

Usage:

"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    // None

Complete Usage Example

import 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
}

Migration from Exception-Based Parsing

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
}

Implementation Notes

  • All parsing methods are null-safe but will throw NullPointerException for null input
  • Methods handle edge cases like "Infinity", "NaN" for floating-point types
  • Implementation delegates to the standard library parsing methods but catches exceptions
  • These methods are backports of Scala 2.13 functionality for earlier versions

Install with Tessl CLI

npx tessl i tessl/maven-org-scala-lang-modules--scala-collection-compat

docs

annotation-backports.md

backported-collections.md

collection-extensions.md

collection-factories.md

index.md

iterator-size-ops.md

java-interop.md

map-extensions.md

method-chaining.md

option-converters.md

resource-management.md

string-parsing.md

tile.json