Skip to content

A Scala macro for automatically extending case classes with additional fields, reducing boilerplate and enhancing model extensibility.

Notifications You must be signed in to change notification settings

EtaCassiopeia/extend-with-macro

Repository files navigation

Extend-With Macro

The @ExtendWith macro is a Scala-based solution designed to alleviate the boilerplate associated with extending case classes to include additional fields. By annotating a base model with @ExtendWith and specifying the new fields, the macro generates an extended model encompassing both the original and the additional fields. This project showcases the implementation of this macro, demonstrating the potent combination of Scala and metaprogramming to automate routine coding tasks.

Features

  • Automatically generate extended case classes with specified additional fields.
  • Reduce boilerplate and adhere to DRY (Don't Repeat Yourself) principle.
  • Enhance model extensibility with minimal manual code intervention.

Installation

To use the @ExtendWith macro in your project, you need to first publish it locally using sbt. Follow the steps below:

  1. Clone the repository to your local machine.
git clone https://github.com/EtaCassiopeia/extend-with-macro.git
cd extend-with-macro
  1. Publish the macro locally using sbt.
sbt publishLocal
  1. Now, you can include the @ExtendWith macro in your Scala project by adding the following line to your build.sbt file:
libraryDependencies += "dev.celestica" %% "extend-with-macro" % "0.1.0"

Usage

After including the @ExtendWith macro in your project, you can use it to extend your case classes as shown below:

import dev.celestica.ExtendWith.Field

@ExtendWith(Field[String]("address"), Field[Int]("age"))
case class Person(name: String)

// Usage of the extended case class
val extendedPerson = Person.ExtendedPerson("John Doe", "123 Street Name", 25)

Further Reading

For a more in-depth description and discussion on the @ExtendWith macro, its implementation, and its usage, feel free to read the accompanying blog post published on Medium: Harnessing Scala Macros to Extend Models: A Deep Dive.

About

A Scala macro for automatically extending case classes with additional fields, reducing boilerplate and enhancing model extensibility.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages