Cool_Dalek

Export example

Feb 11th, 2022 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.79 KB | None | 0 0
  1. import scala.util.Random
  2.  
  3. object Main {
  4.  
  5.   @main def test(): Unit = {
  6.     val printer = Printer()
  7.     val scanner = Scanner()
  8.     val copyMachine = CopyMachine(printer, scanner)
  9.     val file = copyMachine.scanFile()
  10.     copyMachine.printFile(file)
  11.     copyMachine.copy()
  12.     //copyMachine.glitch() - don't compile
  13.   }
  14.  
  15.   class Printer {
  16.  
  17.     def printFile(file: String): Unit =
  18.       println(s"Printing $file.")
  19.  
  20.     def glitch(): Unit =
  21.       println("Glitchy printer")
  22.  
  23.   }
  24.  
  25.   class Scanner {
  26.  
  27.     def scanFile(): String = {
  28.       println("Scanning file")
  29.       Random.nextString(10)
  30.     }
  31.  
  32.   }
  33.  
  34.   class CopyMachine(printer: Printer, scanner: Scanner) {
  35.     export printer.{glitch => _, *}
  36.     export scanner.*
  37.  
  38.     def copy(): Unit = printFile(scanFile())
  39.  
  40.   }
  41.  
  42. }
Add Comment
Please, Sign In to add comment