File tree 5 files changed +56
-0
lines changed
sbt-dotty/sbt-test/sbt-dotty/quoted-example-project
5 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,9 @@ object Toolbox {
17
17
18
18
def make (implicit settings : Settings ): Toolbox = {
19
19
val cl = getClass.getClassLoader
20
+ println(" >>>>>" )
21
+ cl.asInstanceOf [java.net.URLClassLoader ].getURLs().toList.foreach(println)
22
+
20
23
try {
21
24
val toolboxImplCls = cl.loadClass(" dotty.tools.dotc.quoted.ToolboxImpl" )
22
25
val makeMeth = toolboxImplCls.getMethod(" make" , classOf [Settings ])
Original file line number Diff line number Diff line change
1
+ scalaVersion := sys.props(" plugin.scalaVersion" )
2
+
3
+ libraryDependencies ++= Seq (
4
+ " ch.epfl.lamp" % " dotty_0.14" % scalaVersion.value,
5
+ " ch.epfl.lamp" % " dotty_0.14" % scalaVersion.value % " test->runtime" ,
6
+ " com.novocode" % " junit-interface" % " 0.11" % " test"
7
+ )
Original file line number Diff line number Diff line change
1
+ addSbtPlugin(" ch.epfl.lamp" % " sbt-dotty" % sys.props(" plugin.version" ))
Original file line number Diff line number Diff line change
1
+ package hello
2
+
3
+ // Import Expr and some extension methods
4
+ import scala .quoted ._
5
+
6
+ object Main {
7
+
8
+ // Needed to run or show quotes
9
+ implicit val toolbox : scala.quoted.Toolbox = scala.quoted.Toolbox .make
10
+
11
+ def main (args : Array [String ]): Unit = {
12
+ val square = stagedPower(2 )
13
+ println(" 3^2 = " + square(3 ))
14
+ println(" 4.1^2 = " + square(4.1 ))
15
+ println()
16
+ val cube = stagedPower(3 )
17
+ println(" 2.4^3 = " + cube(2.4 ))
18
+ println()
19
+
20
+ val toTheFourth = stagedPower(4 )
21
+ println(" 3^4 = " + toTheFourth(3 ))
22
+ println()
23
+ }
24
+
25
+ def stagedPower (n : Int ): Double => Double = {
26
+ // Code representing the labmda where the recursion is unrolled based on the value of n
27
+ val code = ' { (x : Double ) => $ {powerCode(n, ' {x})}}
28
+
29
+ println(s " staged power for n= " + n + " :" )
30
+ println(code.show)
31
+
32
+ // Evaluate the contents of the code and return it's value
33
+ code.run
34
+ }
35
+
36
+ def powerCode (n : Int , x : Expr [Double ]): Expr [Double ] =
37
+ if (n == 0 ) ' {1.0 }
38
+ else if (n == 1 ) x
39
+ else if (n < 0 ) throw new Exception (" Negative powers not implemented. Left as a small exercise. Dont be shy, try it out." )
40
+ else if (n == 2 ) ' {$x * $x}
41
+ else if (n % 2 == 1 ) ' {$x * $ {powerCode(n - 1 , x)}}
42
+ else ' { val y = $x * $x; $ {powerCode(n / 2 , ' {y})}}
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ > run
You can’t perform that action at this time.
0 commit comments