Top 1,000 Features Creators Events Podcasts Extensions Interviews Blog Explorer CSV

Scala

< >

Scala is an open source programming language created in 2004 by Martin Odersky.

#22on PLDB 20Years Old 219kRepos
Homepage · REPL · Try It Online · Download · Blog · Wikipedia · Subreddit · Twitter · FAQ · Release Notes · Docs · Mailing List

Scala ( SKAH-lah) is a general-purpose programming language providing support for functional programming and a strong static type system. Designed to be concise, many of Scala's design decisions aimed to address criticisms of Java. Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine. Read more on Wikipedia...


Example from Compiler Explorer:
// Type your code here, or load an example. object Square { def square(num: Int): Int = num * num }
Example from Riju:
println("Hello, world!")
Example from hello-world:
object HelloWorld extends App { println("Hello World") }
// Hello world in Scala object HelloWorld extends App { println("Hello world!") }
Example from Linguist:
#!/bin/sh exec scala "$0" "$@" !# object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }
Example from Wikipedia:
val urls = List("http://scala-lang.org", "https://github.com/scala/scala") def fromURL(url: String) = scala.io.Source.fromURL(url) .getLines().mkString("\n") val t = System.currentTimeMillis() urls.par.map(fromURL(_)) println("time: " + (System.currentTimeMillis - t) + "ms")
Scala Keywords
abstract case catch class def do else extends false final finally for forSome if implicit import lazy match new null object override package private protected return sealed super this throw trait try true type val var while with yield

Language features

Feature Supported Example Token
Standard Library
println("Hello, World!")
Conditionals
Inheritance
Functions
Exceptions
Classes
While Loops
Booleans true false
MultiLine Comments
/* A comment
*/
/* */
Print() Debugging println
Message Passing
Line Comments
// A comment
//
Type Inference
Operator Overloading
Implicit Arguments
// https://docs.scala-lang.org/tour/implicit-parameters.html
abstract class Monoid[A] {
  def add(x: A, y: A): A
  def unit: A
}

object ImplicitTest {
  implicit val stringMonoid: Monoid[String] = new Monoid[String] {
    def add(x: String, y: String): String = x concat y
    def unit: String = ""
  }
  
  implicit val intMonoid: Monoid[Int] = new Monoid[Int] {
    def add(x: Int, y: Int): Int = x + y
    def unit: Int = 0
  }
  
  def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
    if (xs.isEmpty) m.unit
    else m.add(xs.head, sum(xs.tail))
    
  def main(args: Array[String]): Unit = {
    println(sum(List(1, 2, 3)))       // uses intMonoid implicitly
    println(sum(List("a", "b", "c"))) // uses stringMonoid implicitly
  }
}
Macros
// https://docs.scala-lang.org/scala3/guides/macros/macros.html
import scala.quoted.* // imports Quotes, Expr

def inspectCode(x: Expr[Any])(using Quotes): Expr[Any] =
  println(x.show)
  x
Comments
Partial Application
def add(x: Int, y: Int) = {x+y}; add(1, _: Int)
Strings
"hello world"
"
Case Insensitive Identifiers X
Semantic Indentation X

View source

- Build the next great programming language · About · Resources · Acknowledgements · Part of the World Wide Scroll