Rules for importing libraries

I would like to know if

import scala.util._

is supposed to import all sub-libraries or do I need to type

import scala.util.hashing

in order to access MurmurHash3.stringHash ?

That puts all the names inside util in scope, so if you want to access MurmurHash3.stringHash you need to do it like hashing.MurmurHash3.stringHash because that is the relative name that you have at this moment.

Anyways, you should not do a wildcard import of something as big as scala.util if you only need hashing just import than and use the relative name. Or if you only want MurmurHash3 then do:

import scala.util.hashing.MurmurHash3

MurmurHash3.stringHash
1 Like

It’s worth underlining that all import does is to provide the specified names in-scope without requiring full paths. It has nothing to do with visibility.

This is different from some languages, where importing is about making something available – where it brings a module into scope. That’s not the case in Scala – if something is available, it’s available; if not, not. All import does is make it a bit more convenient to use.

2 Likes

In other words import just creates an alias

1 Like

Exactly so. It’s very convenient for readability, but that’s mostly it. (There are a few edge cases that can occasionally be important, like being able to alias to a different name in order to resolve conflicts, but they’re unusual.)

1 Like