or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

backported-collections.mdcollection-extensions.mdindex.mdjava-conversion.mdresource-management.mdstring-parsing.mdutility-features.md
tile.json

tessl/maven-org-scala-lang-modules--scala-collection-compat_2-12

A compatibility library that makes Scala 2.13 APIs available on Scala 2.11 and 2.12, facilitating cross-building and migration to newer Scala versions.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.scala-lang.modules/scala-collection-compat_2.12@2.13.x

To install, run

npx @tessl/cli install tessl/maven-org-scala-lang-modules--scala-collection-compat_2-12@2.13.0

index.mddocs/

Scala Collection Compat

Scala Collection Compat is a compatibility library that makes Scala 2.13 APIs available on Scala 2.11 and 2.12, facilitating cross-building and migration to newer Scala versions. It provides backported collection types, enhanced collection methods, Java interoperability utilities, and resource management features.

Package Information

  • Package Name: org.scala-lang.modules:scala-collection-compat_2.12
  • Package Type: maven
  • Language: Scala
  • Installation: libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.13.0"

Core Imports

import scala.collection.compat._

This single import provides access to all compatibility features including:

  • Extension methods for existing collections
  • Backported collection types (ArraySeq, LazyList)
  • Safe string parsing methods
  • Collection factory improvements

Basic Usage

import scala.collection.compat._

// Use 2.13-style collection operations on 2.11/2.12
val numbers = List(1, 2, 3, 4, 5)
numbers.to(Vector)  // Instead of numbers.to[Vector]

// Safe string parsing
val maybeInt: Option[Int] = "42".toIntOption
val maybeDouble: Option[Double] = "3.14".toDoubleOption

// Enhanced collection methods
val data = List(1, 2, 3, 4, 5)
val min = data.minOption  // Safe minimum without exceptions
val max = data.maxOption  // Safe maximum without exceptions

// Size comparison operations
if (data.sizeIs > 3) println("Large collection")

// Backported lazy list
val infinite = LazyList.from(1)
val first10 = infinite.take(10).toList

Architecture

Scala Collection Compat is organized around several key components:

  • Cross-Version Compatibility: Provides consistent APIs across Scala 2.11, 2.12, and 2.13 through conditional compilation
  • Extension Methods: Implicit classes that extend existing collection and string types with new functionality
  • Backported Types: Full implementations of Scala 2.13 collection types for older versions
  • Factory Pattern: Unified collection creation patterns using Factory and BuildFrom abstractions
  • Resource Management: Try-with-resources semantics through the Using utility
  • Java Interoperability: Seamless conversion between Scala and Java collections

Capabilities

Collection Extensions

Enhanced methods for existing Scala collections including safe operations, size comparisons, and functional utilities. Provides 2.13-style APIs on older Scala versions.

// Extension methods available through import scala.collection.compat._
def minOption[B >: A](implicit ord: Ordering[B]): Option[A]
def maxOption[B >: A](implicit ord: Ordering[B]): Option[A]
def sizeCompare(otherSize: Int): Int
def sizeIs: SizeCompareOps
def distinctBy[B](f: A => B): Collection[A]
def groupMap[K, B](key: A => K)(f: A => B): Map[K, Collection[B]]

Collection Extensions

Backported Collections

Complete implementations of Scala 2.13 collection types for use on Scala 2.11 and 2.12, including ArraySeq and LazyList with full feature parity.

object ArraySeq {
  def apply[T](elems: T*)(implicit elemTag: ClassTag[T]): ArraySeq[T]
  def from[T](source: TraversableOnce[T])(implicit elemTag: ClassTag[T]): ArraySeq[T]
  def unsafeWrapArray[T](x: Array[T]): ArraySeq[T]
}

object LazyList {
  def apply[A](elems: A*): LazyList[A]
  def from[A](coll: GenTraversableOnce[A]): LazyList[A]
  def iterate[A](start: => A)(f: A => A): LazyList[A]
  def continually[A](elem: => A): LazyList[A]
}

Backported Collections

String Parsing

Safe string parsing methods that return Option types instead of throwing exceptions, providing consistent parsing across all numeric types.

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

String Parsing

Java Collection Conversion

Utilities for converting between Scala and Java collections with both implicit and explicit conversion methods.

import scala.jdk.CollectionConverters._
// Implicit conversions available through .asScala/.asJava extension methods

import scala.jdk.javaapi.CollectionConverters
// Explicit conversion methods for Java interop
def asScala[A](c: ju.Collection[A]): Iterable[A]
def asJava[A](i: Iterable[A]): jl.Iterable[A]

Java Collection Conversion

Resource Management

Automatic resource management with try-with-resources semantics through the Using utility, supporting single and multiple resource management.

object Using {
  def apply[R: Releasable, A](resource: => R)(f: R => A): Try[A]
  def resource[R, A](resource: R)(body: R => A)(implicit releasable: Releasable[R]): A
  def resources[R1: Releasable, R2: Releasable, A](r1: R1, r2: => R2)(body: (R1, R2) => A): A
}

trait Releasable[-R] {
  def release(resource: R): Unit
}

Resource Management

Utility Features

Additional utilities including chaining operations, annotations for cross-compilation, and enhanced regex functionality.

final class ChainingOps[A](self: A) {
  def tap[U](f: A => U): A
  def pipe[B](f: A => B): B
}

class nowarn(value: String = "")
final class unused

implicit class RegexOps(regex: Regex) {
  def matches(source: CharSequence): Boolean
}

Utility Features

Types

Core Type Aliases

type IterableOnce[+X] = scala.collection.TraversableOnce[X]  // 2.11/2.12
type Factory[-A, +C] = CanBuildFrom[Nothing, A, C]           // 2.11/2.12
type BuildFrom[-From, -A, +C] = /* implementation */         // 2.11/2.12

// On Scala 2.13, these are aliases to standard library types
type Factory[-A, +C] = scala.collection.Factory[A, C]       // 2.13
type BuildFrom[-From, -A, +C] = scala.collection.BuildFrom[From, A, C]  // 2.13

Collection Building

trait BuildFrom[-From, -A, +C] {
  def fromSpecific(from: From)(it: IterableOnce[A]): C
  def newBuilder(from: From): mutable.Builder[A, C]
}

trait Factory[-A, +C] {
  def fromSpecific(it: IterableOnce[A]): C
  def newBuilder: mutable.Builder[A, C]
}