Working Within the Console
Your text editor or IDE will allow you to save code to a file, but we need to save files in the right place so the Scala compiler can find them.
If you're working from the Doodle template you should save your code in the directory
How do we use code that we saved to a file from the console?
There is a special command, that only works from the console, that allows us to run code saved in a file.
This command is called
:pasteFootnote load. We follow
:paste with the name of the file we want to run. For example, if we save in the file
src/main/scala/Example.scala the expression
we can then run this code by writing at the console
:paste src/main/scala/Example.scala // res0: doodle.core.Image = ContextTransform(<function1>,ContextTransform(<function1>,Circle(100.0)))
Note the value has been given the name
res0 in the example above. If you're following along, the name in your console might end with a different number depending on what you've already typed into the console. We can draw the image by evaluating
res0.draw (or the correct name for your console).
Tips for Using the Console
Here are a few tips for using the console more productively:
If you press the up arrow you'll get the last thing you typed into the console. Handy to avoid having to type in those long file names over and over again! You can press up multiple times to go through the history of your interactions at the console.
You can press the
Tabkey to get the console to suggest completions for code, but unfortunately not file names, you're typing. For example, if you type
Striand then press
Tab, the console will show possible completions. Type
Strinand the console will complete
|[load]||There is also a command called
<div class="callout callout-warn"> Once we start saving code to a file, we'll likely find the compiler doesn't like our code next time we start SBT. Read the next section to see how we can fix this problem. </div>