CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-softwaremill-macwire--macros

Zero-cost, compile-time, type-safe dependency injection library providing macros for automatic instance creation in Scala applications

Pending
Overview
Eval results
Files

MacWire Macros

MacWire Macros is a zero-cost, compile-time, type-safe dependency injection library for Scala. It provides compile-time macros that generate instance creation code automatically while maintaining type safety and eliminating runtime overhead. The library supports multiple wiring strategies, from context-free dependency injection to context-dependent resolution using surrounding scope.

Package Information

  • Package Name: com.softwaremill.macwire:macros
  • Package Type: Maven (Scala library)
  • Language: Scala (2.12, 2.13, 3)
  • Installation: "com.softwaremill.macwire" %% "macros" % "2.6.6" % "provided"
  • Util Module (for dynamic instance access): "com.softwaremill.macwire" %% "util" % "2.6.6"
  • Platforms: JVM, JavaScript (Scala.js), Native (Scala Native)

Core Imports

import com.softwaremill.macwire.*

For Scala 2:

import com.softwaremill.macwire._

Basic Usage

import com.softwaremill.macwire.*

// Define your classes
class DatabaseAccess()
class SecurityFilter()
class UserFinder(databaseAccess: DatabaseAccess, securityFilter: SecurityFilter)
class UserStatusReader(userFinder: UserFinder)

// Context-free wiring with autowire (Scala 3)
val userStatusReader = autowire[UserStatusReader]()

// Context-dependent wiring
trait UserModule {
  lazy val databaseAccess = new DatabaseAccess()
  lazy val securityFilter = new SecurityFilter()
  lazy val userFinder = wire[UserFinder]
  lazy val userStatusReader = wire[UserStatusReader]
}

Architecture

MacWire operates through several key mechanisms:

  • Compile-Time Code Generation: All wiring happens at compile time via Scala macros, generating plain new instance creation code
  • Type-Safe Resolution: Uses Scala's type system to resolve dependencies, providing compile-time error detection
  • Multiple Wiring Strategies: Context-free (autowire), context-dependent (wire), and recursive (wireRec) approaches
  • Factory Support: Custom factory functions can be wired using wireWith family of functions
  • Set Collection: Automatic collection of multiple instances of the same type using wireSet

Capabilities

Context-Free Wiring

Context-free dependency injection using explicitly provided dependencies. Creates instances without relying on surrounding scope.

// Scala 3 only
inline def autowire[T](inline dependencies: Any*): T

Context-Free Wiring

Context-Dependent Wiring

Creates instances using dependencies available in the surrounding context (trait/class/object scope).

// Basic wiring
def wire[T]: T

// Recursive wiring - creates missing dependencies automatically
def wireRec[T]: T

// Set collection - gathers all instances of type T
def wireSet[T]: Set[T]

Context-Dependent Wiring

Factory-Based Wiring

Wires dependencies for factory functions, supporting functions with up to 22 parameters.

def wireWith[RES](factory: () => RES): RES
def wireWith[A, RES](factory: (A) => RES): RES
def wireWith[A, B, RES](factory: (A, B) => RES): RES
// ... overloads for up to 22 parameters

Factory-Based Wiring

Dynamic Instance Access

Creates dynamic instance lookup capabilities from object members. Note: Only fully implemented in Scala 2.

def wiredInModule(in: AnyRef): Wired  // Scala 2 implementation only

Dynamic Instance Access

Types

// From util module - requires separate dependency
import com.softwaremill.macwire.Wired

Error Handling

MacWire provides compile-time error detection for:

  • Missing Dependencies: When required constructor parameters cannot be resolved
  • Ambiguous Dependencies: When multiple instances of the same type are available in context-dependent wiring
  • Cyclic Dependencies: When recursive dependencies create circular references
  • Unsupported Types: When attempting to wire primitive types or unsupported type patterns

All errors are reported at compile time with descriptive messages indicating the specific issue and location.

Install with Tessl CLI

npx tessl i tessl/maven-com-softwaremill-macwire--macros
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.softwaremill.macwire/macros_2.13@2.6.x