Software Engineering from Scratch by Jason Lee Hodges

Software Engineering from Scratch by Jason Lee Hodges

Author:Jason Lee Hodges
Language: eng
Format: epub, pdf
ISBN: 9781484252062
Publisher: Apress


Terminal Output

> scalac messenger.scala messages.scala

> scala examples.messenger

Hello World!

Listing 9-2Examples of referencing and compiling multiple files from the same package

You might notice that the extension for the files in this example is no longer a .sc extension but rather a .scala extension. Since these files are not going to be interpreted but rather always compiled, it is better to use the .scala file extension, which is equivalent to the Java .java extension and is meant for objects and classes, rather than the Scala script extension.

The two files in this example both start with a declaration that they belong to the package “examples.” This gives them access to each other’s members implicitly. Thus, when the messenger file wants to access a member of the messages object, it can do so as if the code were written in the same file. You’ll notice that only the messenger object uses the extends App keywords as that is the entry point for our now multi-file program. We haven’t declared anything to happen in the messages file, so its purpose is simply to exist as a reference for our main program, messenger, to make a call to one of its members. Once both files have been written, they are compiled at the same time using the command scalac messenger.scala messages.scala which creates a package directory that contains the compiled class files for both files. If you make a change to one of these files and not the other and need to re-compile, you only need to include the name of the changed file after your scalac command. To execute our program, we simply call the main entry point file from its package directory using the same dot notation that you would use to access the member of a class or object. The command scala examples.messenger looks in the package examples for the messenger object and executes it. This prints out the message “Hello World!” which is a string member that exists in the messages object.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.