First thing to note about packages in Scala is that you cannot simply chuck a bunch of functions and variables inside a package and expect everything to work as expected.
All the behavior you need has to be wrapped in class or object construct. Bare functions are not allowed.
As a developer I want to be able to share a useful method across different files.
Decide if you want the functionality to be static or instance based. In the case below I will go with static, no need for a specific instance.
Directory organization is up to you, Scala does not enforce a specific directory structure for packages, but being organized has its benefits.
A directory structure like this this would work:
└── /src └── /useful └── utils.scala
My intent is to create a directory
/userful with a file called
utils.scala that will house all the methods I want to share. Yes, this can turn into a junk drawer but this is simply an example to help get things wired up.
Now inside the Scala file
utils.scala you will want to do something like:
Now this by itself is not really useful. To use it in some other file:
└── /src └── /things └── speaker.scala
You need to
import it. So in the top of
speaker.scala do this:
._ notation allows you to pull in all the methods in the
Now you can use the use the packaged method like:
A method can be declared
private then it can only be run inside the package it was tagged with and no where else. Here is an example:
Here we are calling
chop on knife from inside of the
kitchen package. This would not work from inside of any other package but kitchen because we have used
private[kitchen] in front of the def chop method definition .
Lets say you call a method from the wrong package (package it has not been tagged with). It will fail to compile:
Package members can be selectively renamed to make dealing with the code a bit easier. The snip below illustrates how a poorly named
knife can actually be used as
Packages can have members totally hidden if need be.
The following completely hides the member from being used:
Now in the code the only
Knife that can be used is the one in