labeled-tostring

Scala traits that override toString to display parameter labels

License

License

GroupId

GroupId

com.yuvimasory
ArtifactId

ArtifactId

labeled-tostring_2.9.0-1
Last Version

Last Version

0.5.0
Release Date

Release Date

Type

Type

jar
Description

Description

labeled-tostring
Scala traits that override toString to display parameter labels
Project URL

Project URL

https://github.com/ymasory/labeled-tostring
Project Organization

Project Organization

com.yuvimasory
Source Code Management

Source Code Management

https://github.com/ymasory/labeled-tostring.git

Download labeled-tostring_2.9.0-1

How to add to project

<!-- https://jarcasting.com/artifacts/com.yuvimasory/labeled-tostring_2.9.0-1/ -->
<dependency>
    <groupId>com.yuvimasory</groupId>
    <artifactId>labeled-tostring_2.9.0-1</artifactId>
    <version>0.5.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.yuvimasory/labeled-tostring_2.9.0-1/
implementation 'com.yuvimasory:labeled-tostring_2.9.0-1:0.5.0'
// https://jarcasting.com/artifacts/com.yuvimasory/labeled-tostring_2.9.0-1/
implementation ("com.yuvimasory:labeled-tostring_2.9.0-1:0.5.0")
'com.yuvimasory:labeled-tostring_2.9.0-1:jar:0.5.0'
<dependency org="com.yuvimasory" name="labeled-tostring_2.9.0-1" rev="0.5.0">
  <artifact name="labeled-tostring_2.9.0-1" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.yuvimasory', module='labeled-tostring_2.9.0-1', version='0.5.0')
)
libraryDependencies += "com.yuvimasory" % "labeled-tostring_2.9.0-1" % "0.5.0"
[com.yuvimasory/labeled-tostring_2.9.0-1 "0.5.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.9.0-1
commons-lang : commons-lang jar 2.3

test (1)

Group / Artifact Type Version
org.scalatest : scalatest jar 1.4.RC2

Project Modules

There are no modules declared in this project.

Labeled ToString

Build Status

Overview

The Labeled ToString project provides several traits you can mix into case classes in order to get toString representations that include parameter labels. That means you get Person(name=John Doe,age=30) instead of Person(John Doe,30).

API Docs (ScalaDocs)

Example

Here's a normal case class:

case class Person(name: String, age: Int)
Person("John Doe", 30).toString
//result is "Person(John Doe,30)"

Here's our labeled case class:

import com.yuvimasory.tostring._
case class Person(name: String, age: Int) extends LabeledToStringDef
Person("John Doe", 30).toString
//result is  "Person(name=John Doe,age=30)"

Installation

labeled-tostring is hosted on Maven Central. You can add it as a dependency to your build.sbt file. It is built for Scala 2.8.0 through 2.9.2.

libraryDependencies += "com.yuvimasory" %% "labeled-tostring" % "0.5.0"

Choosing a trait

The com.yuvimasory.tostring package provides three traits: LabeledToStringDef, LabeledToStringVal, and LabeledToStringLazyVal. They override the default case class's toString method with a def, val, and lazy val, respectively.

  • If you're not sure which to use, start with LabeledToStringDef, which works in all cases.
  • Consider LabeledToStringVal if you know the case class's parameters are either immutable (e.g., primitive types, immutable collections), or have string representations that never change (e.g., arrays).
  • Try LabeledToStringLazyVal if you meet the criteria for LabeledToStringVal and want lazy initialization.
  • Both of the *Val traits may run toString much faster after the first call, since the store the result. The cost is a bit of memory and a slow initial toString call.
  • You must use LabeledToStringDef if your case classes are Squeryl tables. If you don't Squeryl will generate bogus SQL.

Benchmarks

Benchmarks are in a separate repository.

Object creation

[info]                          benchmark      ns linear runtime
[info]              CreateCaseClassPerson    6.66 =
[info]     CreateLabeledToStringValPerson 1262.54 ==============================
[info] CreateLabeledToStringLazyValPerson    8.68 =
[info]     CreateLabeledToStringDefPerson    6.63 =

First toString call

We don't have a good way to benchmark this presently.

Subsequent toString calls

[info]                                         benchmark   ns linear runtime
[info]              SubsequentToStringsOfCaseClassPerson  750 ========
[info]     SubsequentToStringsOfLabeledToStringValPerson  146 =
[info] SubsequentToStringsOfLabeledToStringLazyValPerson  185 ==
[info]     SubsequentToStringsOfLabeledToStringDefPerson 2594 ==============================

Warning

  • These traits produce unexpected strings in the REPL due to the way the REPL wraps code and mangles names.
  • These traits do not work if you add bodies to your case classes (i.e., additional methods or fields).

Versions

Version
0.5.0