Skip to content

Latest commit

 

History

History

plugin-examples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Scala 3 plugin examples

Dotty project Directory plugin-examples\ contains Scala 3 code examples written by myself.

We present how to write/execute Scala 3 plugins in the following code examples:

  • DivideZero generates an error message when the plugin detects a division by zero.
  • ModifyPipeline
  • MultiplyOne removes a multiply operation when the plugin detects that one of the operands is 1.

🔎 As a reminder we have to perform two compilation tasks for each example:

  1. compilation of the plugin source files (e.g. target\DivideZero.jar)
  2. compilation ot the test source files with the plugin enabled/disabled.

DivideZero Example

Command build with no parameter displays the help message:

> build
Usage: build { <option> | <subcommand> }

  Options:
    -debug           show commands executed by this script
    -explain         set compiler option -explain
    -explain-types   set compiler option -explain-types
    -tasty           compile both from source and TASTy files
    -timer           display total execution time
    -verbose         display progress messages

  Subcommands:
    clean            delete generated class files
    compile          compile Scala source files
    doc              generate HTML documentation
    help             display this help message
    pack             create Java archive file
    test             execute unit tests
    test:noplugin    execute unit tests with NO plugin

First let us try subcommand test:noplugin to see the output when the plugin is disabled for the test:

> build clean test:noplugin
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at DivideZeroTest$.main(DivideZeroTest.scala:6)
        at DivideZeroTest.main(DivideZeroTest.scala)

Command build test generates the Scala 3 plugin DivideZero.jar from source file DivideZero.scala and tests the plugin with source file DivideZeroTest.scala:

> build clean test
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:6:12
6 |    println(a / zero)  // error: divide by zero
  |            ^^^^^^^^
  |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:14:12
14 |    println(i / zero)  // error: divide by zero
   |            ^^^^^^^^
   |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:15:12
15 |    println(l / 0)     // error: divide by zero
   |            ^^^^^
   |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:16:12
16 |    println(s / 0)     // error: divide by zero
   |            ^^^^^
   |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:17:12
17 |    println(f / 0)     // error: divide by zero
   |            ^^^^^
   |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:18:12
18 |    println(d / 0)     // error: divide by zero
   |            ^^^^^
   |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:21:12
21 |    println(x / 0)     // error: divide by zero
   |            ^^^^^
   |            divide by zero
-- Error: W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala:22:12
22 |    println(x / 0.0)   // error: divide by zero
   |            ^^^^^^^
   |            divide by zero
8 errors found
Error: Compilation of test Scala source files failed

🔎 We give two argument files to the Scala 3 compiler: target\test_scalac_opts.txt (compiler options) and target\test_scalac_sources.txt (source files):  

> type target\test_scala*.txt
 
target\test_scalac_opts.txt
 
 
-deprecation -feature -nowarn -Xplugin:"W:\plugin-examples\DivideZero\target\divideZero.jar" -Xplugin-require:divideZero -P:"divideZero:opt1=1" -classpath "W:\plugin-examples\DivideZero\target\classes;W:\plugin-examples\DivideZero\target\test-classes" -d "W:\plugin-examples\DivideZero\target\test-classes"
 
target\test_scalac_sources.txt
 
 
W:\plugin-examples\DivideZero\src\test\scala\DivideZeroTest.scala

In particular we observe the usage of the two plugin options -Xplugin:<plugin_jar_file> and -Xplugin-require:<plugin_name> -P:"divideZero:opt1=1".

ModifyPipeline Example

Command build test generates the Scala 3 plugin ModifyPipeline.jar from source file ModifyPipeline.scala and tests the plugin with source file ModifyPipelineTest.scala:

> build clean test
5
5

MultiplyOne Example

Command build clean test generates the Scala 3 plugin MultiplyOne.jar from source file MultiplyOne.scala and tests the plugin with source file MultiplyOneTest.scala:

> build clean test
5
5
25
25
1.4142135623730951
1.4142135623730951
a
aaaaaaaaaa

mics/September 2023