8

I've found the the following import in some scala:

import Predef.{println => _, _}

What does the => do?

Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
Finkelson
  • 2,921
  • 4
  • 31
  • 49
  • 1
    Possible duplicate of [Scala punctuation (AKA symbols and operators)](http://stackoverflow.com/questions/7888944/scala-punctuation-aka-symbols-and-operators) –  Sep 30 '15 at 23:05

1 Answers1

11

Generally => in an import allows you to alias an existing name into an alternate name:

import scala.{Int => i32}

This would allow you to use i32 in place of Int

Further, importing _ imports all symbols into the current namespaces.

Now, aliasing a name into _, however does the opposite, i.e. excludes it from the import:

import Predef.{println => _, _}

means

*Import all from Predef except println

Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
  • And why I can import only methods from `object` and not a `class`? – Finkelson Sep 30 '15 at 23:23
  • You import the class, which you then have to make an instance of, and *then* you can use the methods on it. That's how classes work. – childofsoong Sep 30 '15 at 23:25
  • You can import methods from the *instance* of a `class`, but until a `class` has been instantiated, the methods really only exist as a recipe. An `object` acts basically like a singleton instantiation of some hidden `class`, hence you can import methods from it – Arne Claassen Sep 30 '15 at 23:26
  • 1
    Correct. Think of `object` imports as Java's static method imports and class instance imports are identical in behavior between *scala* and Java – Arne Claassen Sep 30 '15 at 23:41