Questions tagged [scala]

Scala is a general-purpose programming language principally targeting the Java Virtual Machine. Designed to express common programming patterns in a concise, elegant, and type-safe way, it fuses both imperative and functional programming styles. Its key features are: an advanced static type system with type inference; function types; pattern-matching; implicit parameters and conversions; operator overloading; full interoperability with Java; concurrency

Scala is a general-purpose programming language principally targeting the Java Virtual Machine. Designed to express common programming patterns in a concise, elegant, and type-safe way, it fuses both and styles. Its key features are:

  • Static typing
  • Advanced type system with type inference and declaration-site variance
  • Function types (including anonymous) which support lexical closures
  • Pattern-matching
  • Implicit parameters and conversions which support the typeclass and enrich-my-library patterns
  • Mixin composition
  • Full interoperability with Java
  • Powerful concurrency constructs
  • Advanced language constructs such as delimited continuations and an experimental macro system

For more information, see the official Scala Introduction and Scala Documentation.

To search for Scala symbols such as => in Stack Overflow, you can use symbolhound search.

To search Scala documentation, you can use scalex.

A community list of available libraries is available at Awesome Scala

There is Scala Library Index by Scala Center: Scaladex.

Free Scala programming books and guides

Stack Overflow Scala Tutorial

  1. Introduction to Scala
  1. Variables/values
  1. Methods
  1. Literals, statements and blocks
  1. Loops/recursion
  1. Data structures / Collections
  1. For-comprehension
  1. Enumeration
  1. Pattern matching
  1. Classes, objects and types
  1. Packages, imports and visibility identifiers
  1. Inheritance
  1. Extractors
  1. Case classes
  1. Parameterized types
  1. Traits
  1. Self references
  1. Error handling
  1. Type handling
  1. Annotations
  1. Functions/Function literals
  1. Type safety
  1. Implicits
  1. Reflection
  1. Enrich-my-library pattern (formerly known as pimp-my-library)
  1. Concurrency overview
  2. Actors
  1. Use Java from Scala and vice versa
  1. XML literals
  • Explanation
  1. Scala Swing
  1. Type Programming
  1. Functional Scala

Further learning

  1. Learning Resources
  1. REPL
  1. Working with scalac and scala
  1. Operator precedence
  1. Scala style
  1. Functional Programming Principles in Scala, a functional programming course on Coursera taught by Martin Odersky, the creator of Scala.
  2. Principles of Reactive Programming, a reactive functional programming course on Coursera taught by Martin Odersky, Erik Meijer, Roland Kuhn.
  3. Parallel Programming, a parallel programming course on Coursera taught by Viktor Kuncak and Aleksandar Prokopec.
111781 questions
373
votes
6 answers

What is a sealed trait?

Sealed classes are described in 'Programming in Scala', but sealed traits are not. Where can I find more information about a sealed trait? I would like to know, if a sealed trait is the same as a sealed class? Or, if not, what are the differences?…
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
370
votes
9 answers

What is the formal difference in Scala between braces and parentheses, and when should they be used?

What is the formal difference between passing arguments to functions in parentheses () and in braces {}? The feeling I got from the Programming in Scala book is that Scala's pretty flexible and I should use the one I like best, but I find that some…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
361
votes
7 answers

What is the apply function in Scala?

I never understood it from the contrived unmarshalling and verbing nouns ( an AddTwo class has an apply that adds two!) examples. I understand that it's syntactic sugar, so (I deduced from context) it must have been designed to make some code more…
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
361
votes
19 answers

Read entire file in Scala?

What's a simple and canonical way to read an entire file into memory in Scala? (Ideally, with control over character encoding.) The best I can come up with is: scala.io.Source.fromPath("file.txt").getLines.reduceLeft(_+_) or am I supposed to use…
Brendan OConnor
  • 9,624
  • 3
  • 27
  • 25
355
votes
4 answers

What does "coalgebra" mean in the context of programming?

I have heard the term "coalgebras" several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages that give mathematical description of these…
346
votes
7 answers

Understanding implicit in Scala

I was making my way through the Scala playframework tutorial and I came across this snippet of code which had me puzzled: def newTask = Action { implicit request => taskForm.bindFromRequest.fold( errors =>…
Clive
  • 3,991
  • 3
  • 17
  • 15
344
votes
11 answers

What is Scala's yield?

I understand Ruby and Python's yield. What does Scala's yield do?
Geo
  • 93,257
  • 117
  • 344
  • 520
331
votes
13 answers

What is the difference between a var and val definition in Scala?

What is the difference between a var and val definition in Scala and why does the language need both? Why would you choose a val over a var and vice versa?
Derek Mahar
  • 27,608
  • 43
  • 124
  • 174
329
votes
11 answers

How to use java.String.format in Scala?

I am trying to use a .format method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece: private void checkText(String s) { int idx; //…
Ivan
  • 63,011
  • 101
  • 250
  • 382
316
votes
17 answers

How to show full column content in a Spark Dataframe?

I am using spark-csv to load data into a DataFrame. I want to do a simple query and display the content: val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("my.csv") df.registerTempTable("tasks") results =…
tracer
  • 3,265
  • 2
  • 15
  • 6
316
votes
1 answer

Scala: join an iterable of strings

How do I "join" an iterable of strings by another string in Scala? val thestrings = Array("a","b","c") val joined = ??? println(joined) I want this code to output a,b,c (join the elements by ",").
scala_newbie
  • 3,435
  • 2
  • 12
  • 13
316
votes
7 answers

How to model type-safe enum types?

Scala doesn't have type-safe enums like Java has. Given a set of related constants, what would be the best way in Scala to represent those constants?
Jesper
  • 202,709
  • 46
  • 318
  • 350
309
votes
19 answers

How do I break out of a loop in Scala?

How do I break out a loop? var largest=0 for(i<-999 to 1 by -1) { for (j<-i to 1 by -1) { val product=i*j if (largest>product) // I want to break out here else …
TiansHUo
  • 8,509
  • 7
  • 45
  • 57
305
votes
5 answers

What is a higher kinded type in Scala?

You can find the following on the web: Higher kinded type == type constructor? class AClass[T]{...} // For example, class List[T] Some say this is a higher kinded type, because it abstracts over types which would be compliant with the…
Lutz
  • 4,675
  • 4
  • 18
  • 23
288
votes
1 answer

What are Scala context and view bounds?

In a simple way, what are context and view bounds and what is the difference between them? Some easy-to-follow examples would be great too!
chrsan
  • 3,390
  • 3
  • 19
  • 14