odbhut.shei.chhele
odbhut.shei.chhele

Reputation: 6234

What does "=>" mean in import in scala?

I am new to scala. I am looking through some code and came up with a code that imports com.infinite.usermanagement.controllers.{ SecurityService => BaseSecurityService } package. I was wondering what does => sign means in an import.

Upvotes: 14

Views: 2524

Answers (2)

Peanut
Peanut

Reputation: 3963

This line means you import the class SecurityService and rename it to BaseSecurityService. You can use this to prevent name conflicts, etc. You can use this class by using BaseSecurityService instead of the original class name.

A very common example is the following (to prevent mixing up Scala and Java classes):

import java.util.{Map => JMap, List => JList}

Upvotes: 17

pndc
pndc

Reputation: 3795

As others have mentioned, it's an import rename. There is however one further feature that proves astoundingly-useful on occasion that I would like to highlight: If you "rename" to _, the symbol is no longer imported.

This is useful in a few cases. The simplest is that you'd like to do a wildcard import from two packages, but there's a name that's defined in both and you're only interested in one of them:

import java.io.{ File=>_, _ }
import somelibrary._

Now when you reference File, it will unambiguously use the somelibrary.File without having to fully-qualify it.

In that case, you could have also renamed java.io.File to another name to get it out of the way, but sometimes you really do not want a name visible at all. This is the case for packages that contain implicits. If you do not want a particular implicit conversion (e.g. if you'd rather have a compile error) then you have to delete its name completely:

import somelibrary.{RichFile => _, _}
// Files now won't become surprise RichFiles

Upvotes: 15

Related Questions