Code Monkey home page Code Monkey logo

essential-macros's Introduction

Essential Macros Code Examples

Copyright 2014 Dave Gurnell of Underscore http://underscore.io

These example projects are companion material for the Underscore Essential Scala Macros course as well as for the author's talk, Macros for the Rest of Us, presented at ScalaDays 2014.

Licensed under the Apache License v2.0 http://www.apache.org/licenses/LICENSE-2.0.html

If you're interested in this content, sign up to our mailing list at http://underscore.io where we post updates and code samples about Scala, Scala Macros, and functional programming in general.

The Examples

Each example is split into two projects, app and lib. lib defines the macros and app makes use of them.

  • hello - Hello world written as a macro. The macro embeds a compilation timestamp in the printed message to demonstrate that it is executed at compile time. This is contrasted with regular code that prints a timestamp at run time.

  • maximum - Simple project demonstrating basic setup. The macro itself, maximum, isn't particularly useful, but the project serves as a good example of the various concepts in play.

  • printtree - Macro that uses showCode and showRaw to print the desugared syntax and underlying tree structure of arbitrary snippets of Scala code. Good for doing research in advance of writing new macros.

  • simpleassert - Macro demonstrating simple pattern matching on trees using quasiquotes. Slightly improved version of Scala's built-in assert method that prints various values involved in the assertion.

  • betterassert - Macro demonstrating more advanced tree inspection using pattern matching and tree traversal. Improved version of simpleassert that prints useful debugging information in a much wider range of cases.

  • printtype - Family of generic macros that print a variety of information about their type parameters.

  • orderings - Code generation macro that inspects a type and creates an object allowing sorting by any of its fields. This technique is useful when writing, for example, a web service that allows users to sort a database by any field in the returned data.

  • enumerations - Generating the set of instances of a sealed trait. Useful for rolling your own enumerations via sealed case objects.

  • whitebox - Simple project demonstrating the fundamental difference between whitebox and blackbox macros. See comments in the main application file for details.

  • validation - Sketch of a codebase for a validation library that automatically tags errors using the names of the erroneous fields. One significant property is that the macro is implemented as a chainable method call rather than as a top-level function.

  • csv - Sketch of a type-class-based codebase for CSV serialization. An implicit macro is used to support automatic materialization of type class instances for case classes.

essential-macros's People

Contributors

d6y 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.