Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yingy4/36b3ed8ee460f5fd09bfaef6bca263e2 to your computer and use it in GitHub Desktop.
Save yingy4/36b3ed8ee460f5fd09bfaef6bca263e2 to your computer and use it in GitHub Desktop.
Scala Syntactic Sugar and Desugar in IntelliJ IDEA

Scala Syntactic Sugar and Desugar in IntelliJ IDEA

Syntactic sugar is just a way that we re-write our code to a simple and human-readable form, it does not change any function of a method.

IntelliJ IDEA offers a Desugar Scala Code function, you can use that to discover what this syntactic sugar actually stands for.

Here is an example:

def lift[T1,T2](f: T1 => T2):Try[T1]=>Try[T2] = _ map f

In IntelliJ IDEA, select(highlight) _ map f, click Code->Desugar Scala Code in toolbar:

It will convert the code to something like this:

def lift[T1,T2](f: T1 => T2):Try[T1]=>Try[T2] = (triedT: _root_.scala.util.Try[T1]) => triedT.map(f)

Why this is equvalent? (=== represent equivalent)

(triedT: _root_.scala.util.Try[T1]) => triedT.map(f)
=== (triedT) => triedT.map(f) //Omit the input type, it can be inferred by the compiler
=== _.map(f) //There is only one input, it can be re-write as a _
=== _ map(f) //Omit .
=== _ map (f) //Add a space
=== _ map f //Omit parenthese
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment