GeeCON 2022: Magda Stożek - Say goodbye to implicits - contextual abstractions in Scala 3
GeeCON 2022: Magda Stożek - Say goodbye to implicits - contextual abstractions in Scala 3.
Summary
### Overview At GeeCON 2022, Magda Stożek discusses the overhaul of contextual abstractions in Scala 3, focusing on the elimination of the overloaded `implicit` keyword. Reflecting on her five years of experience as a Scala developer, Stożek highlights how implicits in Scala 2 caused confusion due to a single keyword serving multiple disparate roles, sneaky implicit conversions, and cryptic compiler errors. Scala 3 redesigns this system by replacing `implicit` with dedicated, purpose-built language constructs designed to improve readability and developer experience.
### Key Topics The talk contrasts Scala 2 and Scala 3 across seven concrete use cases: - **Providing Context**: Replacing implicit parameters and values with the `using` and `given` keywords. - **Adding Functionality**: Replacing implicit classes with explicit `extension` methods. - **Type Classes**: Implementing ad-hoc polymorphism using `given` definitions and resolving instances via `summon` instead of `implicitly`. - **Conversions**: Replacing implicit methods with implementations of the dedicated `scala.Conversion` class. - **Imports**: Introducing separate import syntax (`import module.given`) to distinguish contextual givens from normal wildcard imports (`import module.*`). - **Unused Names**: Allowing anonymous givens to avoid naming variables required only by the compiler. - **Compiler Errors**: Improved compiler messages that clearly identify where and why contextual parameters are missing, including import suggestions.
### Technical Details - **Contextual Parameters**: In Scala 2, context (such as an `ExecutionContext` for `Future`) relied on `implicit val` and an `(implicit ec: ExecutionContext)` parameter list. In Scala 3, this is defined using `(using ec: ExecutionContext)` and supplied via `given`. - **Extensions**: Instead of wrapping types in `implicit class Qualifies(book: Book)` to add methods to closed types, Scala 3 directly declares `extension (b: Book) def qualifiesForChallenge: Boolean`. - **Type Classes & Summoning**: Type class instances are declared via `given Scorable[Book] with ...`. When using context bounds (`[T: Scorable]`), Scala 3 replaces `implicitly[Scorable[T]]` with the `summon[Scorable[T]]` method. - **Conversions**: Rather than defining `implicit def bookToItem(b: Book): Item`, Scala 3 requires defining a `given Conversion[Book, Item]` with an `apply` method, preventing ambiguous conversion definitions. - **Scoped Imports**: In Scala 2, wildcard imports imported both normal members and implicits. In Scala 3, `.*` imports explicit members only, while `.given` must be used to bring `given` instances into scope. - **Migration and Interoperability**: Scala 3 allows gradual migration; in Scala 3.0 and 3.1, `given` definitions and `implicit` definitions can interoperate via compiler flags before deprecation warnings and errors are introduced in later versions.
### Takeaways Scala 3 splits the overloaded responsibilities of the `implicit` keyword into distinct mechanisms (`given`, `using`, `extension`, and `Conversion`). This shift aligns with functional programming principles by clearly expressing intent rather than implementation mechanics. Teams can adopt Scala 3 incrementally without rewriting all contextual code at once.
Related talks
PROIDEA Events·31 min
Industry experts analyze emerging threats surrounding JDD 2022: Magda Stożek - Say goodbye to implicits - contextual abstractions in Scala 3.
PROIDEA Events·38 min
Rick Ossendrijver
Analysts investigate attack surfaces and defenses concerning Rick Ossendrijver: Say goodbye to bugs and anti-patterns with Error Prone | JDD 2023.
PROIDEA Events·5 min
Magda Stożek
Presenters detail vulnerability discovery and exploitation in JDD2015 - Interview with Magda Stożek.


