Giter VIP home page Giter VIP logo

dotty-macro-examples's Introduction

Scala 3 Metaprogramming Examples

CI

This repo contains a bunch of examples of doing metaprogramming in Scala 3 using the low level reflection API.

Every folder is a separate example. Each example contains a README.md file with a description of what the example does.

To run an example:

  1. Clone and cd into the repo using git clone https://github.com/anatoliykmetyuk/dotty-macro-examples.git && cd dotty-macro-examples

  2. Use ./mill <example_name>.run command to run the example you are interested in. E.g. ./mill macroTypeClassDerivation.run runs macroTypeClassDerivation example.

Examples

  • abstractTypeclassBody – how to abstract a body of a function inside a macro-generated class into a separate macro.
  • accessMembersByName – access an arbitrary member of a value given this member's name as a String.
  • accessEnclosingParameters - access the arguments passed to the enclosing method of the macro call.
  • defaultParamsInference – given a case class with default parameters, obtain the values of these default parameters.
  • fullClassName - get a fully qualified name of a class.
  • isMemberOfSealedTraitHierarchy - check if a class inherits from a sealed trait.
  • macroTypeClassDerivation – typeclass construction done with Quotes Reflection.
  • outOfScopeMethodCall – get a reference to this where the type of this may not be known on macro definition site, and call a method on this.
  • outOfScopeTypeParam – get a reference to a type that is not available to the macro definition's scope. Then use this reference as a type parameter to call a method.
  • outOfScopeClassConstructor – get a reference to a type that is not available to the macro definition's scope. Then use this reference to construct an instance of that type via new.
  • buildingCustomASTs – Quotes Reflection ASTs are powerful, but how do you know the right way to build one? This example demonstrates how to inspect compiler-generated ASTs for a given Scala code. You can then mimic the compiler when constructing similar ASTs.
  • contextParamResolution – showcases how to use Quotes Reflection to construct an AST for a method call that takes context parameters. Shows how to resolve those parameters using Quotes Reflection API.
  • passVarargsIntoAST - showcases how to pass varargs as parameters into the AST of the method
  • primaryConstructor - showcases how to use primary constructor Symbol and Term.
  • referenceVariableFromOtherExpr - how to use a variable at an Expr other than where it is defined at.
  • reflectionLambda - how to create a lambda via TASTy Reflection.

Tips and Tricks

  • When working with reflect API, all of the API available to you is defined in the dotty/library/src/scala/quoted/Quotes.scala. As a rule, for every reflected type X, you have a group of extension methods in trait XMethods which defines all of the methods you can call on X. For example, for Symbol, you can search for SymbolMethods in Quotes.scala to see what you can do with it.
  • TypeTree.of[T] gives you a quotes.reflect.TypeTree. To get a quotes.reflect.TypeRepr, you can call TypeRepr.of[T].
  • Most of the interesting data about the types reside in their Symbols. To get a Symbol given a tpe: quoted.Type[T], use TypeTree.of[T].symbol.
  • If you have a Symbol, you can use Ref to get a Tree referring to that symbol – either Ident or Select. For example, if you have a sym: Symbol that refers to a DefDef method definition and you want to obtain a tree referring to that method, you can get this tree via Ref(sym).

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.