README.md 3.0 KB
Newer Older
梦境迷离's avatar
梦境迷离 已提交
1 2 3 4
# scala-macro-tools [![Build](https://github.com/jxnu-liguobin/scala-macro-tools/actions/workflows/ScalaCI.yml/badge.svg)](https://github.com/jxnu-liguobin/scala-macro-tools/actions/workflows/ScalaCI.yml)

Motivation
--
梦境迷离's avatar
梦境迷离 已提交
5 6 7

scala macro and abstract syntax tree learning code.

梦境迷离's avatar
梦境迷离 已提交
8 9 10
# Features

## @toString
梦境迷离's avatar
梦境迷离 已提交
11

梦境迷离's avatar
梦境迷离 已提交
12
- Note
梦境迷离's avatar
梦境迷离 已提交
13 14
    - `verbose` Whether to enable detailed log.
    - `withFieldName` Whether to include the name of the field in the toString.
梦境迷离's avatar
pre  
梦境迷离 已提交
15 16
    - `withInternalField` Whether to include the fields defined within a class.
    - Support `case class` and `class`.
梦境迷离's avatar
梦境迷离 已提交
17

梦境迷离's avatar
pre  
梦境迷离 已提交
18
- Example
梦境迷离's avatar
梦境迷离 已提交
19

梦境迷离's avatar
梦境迷离 已提交
20
```scala
梦境迷离's avatar
梦境迷离 已提交
21 22 23 24 25
class TestClass(val i: Int = 0, var j: Int) {
  val y: Int = 0
  var z: String = "hello"
  var x: String = "world"
}
梦境迷离's avatar
梦境迷离 已提交
26

梦境迷离's avatar
pre  
梦境迷离 已提交
27
println(new TestClass(1, 2));
梦境迷离's avatar
梦境迷离 已提交
28
```
梦境迷离's avatar
梦境迷离 已提交
29

梦境迷离's avatar
梦境迷离 已提交
30
|withInternalField / withFieldName| false  |true
梦境迷离's avatar
梦境迷离 已提交
31
|  ---------------------------------  | ----------------------------------  |----------------------------------|
梦境迷离's avatar
pre  
梦境迷离 已提交
32
|false|```TestClass(1, 2)``` |```TestClass(i=0, j=2)```|
梦境迷离's avatar
梦境迷离 已提交
33 34 35 36 37 38 39 40 41 42
|true|```TestClass(1, 2, 0, hello, world)```|```TestClass(i=1, j=2, y=0, z=hello, x=world)```|

# How to use

Add library dependency
```scala
"io.github.jxnu-liguobin" %% "scala-macro-tools" % "<VERSION>"
```


梦境迷离's avatar
梦境迷离 已提交
43
The artefacts have been uploaded to Maven Central.
梦境迷离's avatar
梦境迷离 已提交
44 45 46 47

| Library Version | Scala 2.11 | Scala 2.12 | Scala 2.13 |
|---------|------------|------------|------------|
|0.0.1|-|-|[![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.13.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.jxnu-liguobin%22%20AND%20a:%22scala-macro-tools_2.13%22)|
梦境迷离's avatar
梦境迷离 已提交
48 49
|0.0.2|[![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.11.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.jxnu-liguobin%22%20AND%20a:%22scala-macro-tools_2.11%22)|[![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.12.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.jxnu-liguobin%22%20AND%20a:%22scala-macro-tools_2.12%22)|[![Maven Central](https://img.shields.io/maven-central/v/io.github.jxnu-liguobin/scala-macro-tools_2.13.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.jxnu-liguobin%22%20AND%20a:%22scala-macro-tools_2.13%22)|

梦境迷离's avatar
梦境迷离 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

Importing the library into your build system (e.g gradle, sbt), is not enough. You need to follow an extra step.

| Scala 2.11 | Scala 2.12 | Scala 2.13 |
|------------|-------------|------------|
| Import macro paradise plugin  | Import macro paradise plugin | Enable compiler flag `-Ymacro-annotations` required |

```scala
addCompilerPlugin("org.scalamacros" % "paradise_<your-scala-version>" % "<plugin-version>")
```

Where `<your-scala-version>` must be the full scala version. For example 2.12.13, and not 2.12.

If that doesn't work, google for alternatives.

In version scala`2.13.x`, the functionality of macro paradise has been included in the scala compiler directly. 
However, you must still enable the compiler flag `-Ymacro-annotations`. Please see examples from `examples212` sub-project.