Skip to content

Add dotty-staging.g8 in the staging documentation #7165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions docs/docs/reference/metaprogramming/staging.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def run[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): T = ..
def withQuoteContext[T](thunk: given QuoteContext => T) given (toolbox: Toolbox): T = ...
```

## Create a new Dotty project with staging enabled

```shell
sbt new lampepfl/dotty-staging.g8
```

From [lampepfl/dotty-staging.g8](https://github.com/lampepfl/dotty-staging.g8).

It will create a project with the necessary dependencies and some examples.

## Example

Now take exactly the same example as in [Macros](./macros.md). Assume that we
Expand All @@ -84,7 +94,7 @@ to get a source-like representation of the expression.
import scala.quoted.staging._

// make available the necessary toolbox for runtime code generation
delegate for Toolbox = Toolbox.make(getClass.getClassLoader)
given as Toolbox = Toolbox.make(getClass.getClassLoader)

val f: Array[Int] => Int = run {
val stagedSum: Expr[Array[Int] => Int] = '{ (arr: Array[Int]) => ${sum('arr)}}
Expand All @@ -99,11 +109,12 @@ Note that if we need to run the main (in an object called `Test`) after
compilation we need make available the compiler to the runtime:

```shell
sbt:dotty> dotr -classpath out -with-compiler Test
dotc -with-compiler -d out Test.scala
dotr -with-compiler -classpath out Test
```

Or, from SBT:

```scala
libraryDependencies += "ch.epfl.lamp" %% "dotty-compiler" % scalaVersion.value
libraryDependencies += "ch.epfl.lamp" %% "dotty-staging" % scalaVersion.value
```