Giter VIP home page Giter VIP logo

scala-nameof's Introduction

"nameOf" macro for scala

Get the name of an variable, class member, or type as a string--at compile-time!

Inspired by the nameof operator in C#

Used to obtain the simple (unqualified) string name of a variable, type, or member. When reporting errors in code, hooking up model-view-controller (MVC) links, firing property changed events, etc., you often want to capture the string name of a method. Using nameof helps keep your code valid when renaming definitions. Before you had to use string literals to refer to definitions, which is brittle when renaming code elements because tools do not know to check these string literals.

Usage

Add the library as "provided", because it's only needed during compilation and not at runtime:

libraryDependencies += "com.github.dwickern" %% "scala-nameof" % "1.0" % "provided"

Now you can use nameOf to get the name of a variable or class member:

  import com.github.dwickern.macros.NameOf._

  case class Person(name: String, age: Int)

  def toMap(person: Person) = Map(
    nameOf(person.name) -> person.name,
    nameOf(person.age) -> person.age
  )

  // compiles to:

  def toMap(person: Person) = Map(
    "name" -> person.name,
    "age" -> person.age
  )

To get the name of a function:

  import com.github.dwickern.macros.NameOf._

  def startCalculation(value: Int): Unit = {
    println(s"Entered ${nameOf(startCalculation _)}")
  }

  // compiles to:

  def startCalculation(value: Int): Unit = {
    println(s"Entered startCalculation")
  }

Without having an instance of the type:

  import com.github.dwickern.macros.NameOf._

  case class Person(name: String, age: Int)

  println(nameOf[Person](_.age))

  // compiles to:

  println("age")

You can also use nameOfType to get the unqualified name of a type:

  import com.github.dwickern.macros.NameOf._

  println(nameOf[java.lang.String])

  // compiles to:

  println("String")

Development

To run tests for all compilation targets:

sbt +test

To publish to your local ivy repository:

sbt +publishLocal

To publish to maven central:

sbt +publishSigned

License

See LICENSE (MIT).

scala-nameof's People

Contributors

dwickern avatar

Watchers

James Cloos avatar Riddle 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.