Code Monkey home page Code Monkey logo

better-tostring's Introduction

better-tostring

License Latest version Maven Central

A Scala compiler plugin that replaces the default toString implementation of case classes with a more verbose one.

Example

Without the plugin:

final case class User(name: String, age: Int)

User("Joe", 18).toString() // "User(Joe, 18)"

With the plugin:

User("Joe", 18).toString() // "User(name = Joe, age = 18)"

Usage

In sbt:

libraryDependencies += compilerPlugin("org.polyvariant" % "better-tostring" % version cross CrossVersion.full)

In scala-cli:

//> using plugin "org.polyvariant:::better-tostring:version"

(note: versions before 0.3.8 were published under the com.kubukoz organization instead of org.polyvariant)

The plugin is currently published for the following Scala versions:

  • 2.12.15, 2.12.17, 2.12.18
  • 2.13.10, 2.13.11, 2.13.12
  • 3.1.3
  • 3.2.2
  • 3.3.0-RC6, 3.3.0, 3.3.1-RC1, 3.3.1, 3.3.2-RC1
  • 3.4.0-RC1

For older Scala versions, see previous versions of better-tostring (or even older versions).

As a rule of thumb, active support will include at least 3 latest stable versions of 2.12, 2.13 and 3.0 for the foreseeable future.

What does the plugin actually do?

  1. Only case classes located directly in packages or objects are changed. Nested classes and classes local to functions are currently ignored.
  2. Only the fields in the first parameter list are shown.
  3. If the class is nested in an object (but not a package object), the object's name and a dot are prepended.
  4. If the class already overrides toString directly, it's not replaced.

Roadmap

  • Ignore classes that inherit toString from a type that isn't Object (#34)
  • Potentially ignore value classes (#19)

If you have ideas for improving the plugin, feel free to create an issue and we'll consider making it happen :)

Customization?

tl;dr there is none.

The plugin makes certain assumptions about what is a better toString. We aim for a useful and reasonably verbose description of the data type, which could make it easier to find certain issues with your tests (mismatching values in a field) or see the labels in debug logs.

We also want the plugin to become minimal in the implementation and easy to use (plug & play), without lots of configuration options, so the representation of the data types will not be customizable. The format may change over time without prior notice, so you shouldn't rely on the exact representation (as is the case with any toString methods), but any changes in behavior will be communicated in the release notes.

If you need a different toString, we suggest that you implement one yourself. You may also want to look at pprint.

Contributing

If you want to add a new feature, check if it's already been discussed in the issues list.

Before you start working on an existing feature / bugfix, let us know you're taking it on in its comments :)

To add a new Scala version:

  1. Add it to ./scala-versions
  2. Run sbt generateAll
  3. Commit and open a pull request.

Maintainers

The maintainers of this project (people who can merge PRs and make releases) are:

Community

This project supports the Scala code of conduct and wants communication on all its channels (GitHub etc.) to be inclusive environments.

If you have any concerns about someone's behavior on these channels, contact Jakub Kozłowski.

better-tostring's People

Contributors

kubukoz avatar scala-steward avatar mergify[bot] avatar mrobakowski avatar majk-p avatar keynmol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.