Project Structure (SBT)
I’ve been spending more time reading Programming in Scala than working through Exercism problems recently. I realized that to properly practice and understand the concepts, I needed to leave the command line behind and start a (small) project.
The most common Scala project structure is SBT or “Simple Build Tool.” I’ve been using the IntelliJ IDE which has an SBT plugin that allows user to quickly and easily create a project that follows SBT conventions. Assuming you have the Scala and SBT plugins installed, it’s as simple as opening Intellij, selecting “Create New Project” > “Scala” > “sbt”.
My Project Structure
The project I’ll be working on is an implementation of various finite difference numerical integration schemes for simple functions. So my starting SBT structure looks like
- FiniteDifferences
- project
- build.properties
- src
- main
- scala
- test
- scala
- build.sbt
The entry point to my program, Main.scala, belongs in src/main/scala. I’ll need to create a package with several subpackages and/or classes (e.g. Polynomials, TrapezoidMethod, etc.) to support the program. Where should this code go?
According to the docs and stackoverflow the base package for an organization such as Google would follow the structure
GoogleProject/src/main/scala/com/google/googleproject/package1/class1.scala
My structure then looks like
FiniteDifferences/src/main/scala/com/ztbarry/finitedifferences/nicefunctions/polynomials.scala
Writing A Package
I want to make a package of “nice” functions that my finite difference methods will accept as input. One class of these functions will be polynomials. Given the file structure above, the file specifying this class should look like
package org.ztbarry.finitedifferences.nicefunctions
class Polynomial {
}
The package declaration tells the compiler that everything following should be put in the com.ztbarry.finitedifferences.nicefunctions
project. Everything inside this project will also be visible within the file I’ve started above. So if I want to reference some other class from the nicefunctions
project, say ContinuousFunctions, I can do that here.
Next Steps
Now that I have the structure in place, I can start the fun part - implementing the function classes and finite difference classes. Stay tuned! This project will be hosted on Github.