Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between ++= and += in sbt, say with libraryDependencies?

Tags:

scala

sbt

I'm new to Scala and sbt and I can't understand what the differences are between:

libraryDependencies ++= Seq(...)

and

libraryDependencies += ...
like image 543
Gigitsu Avatar asked Jun 27 '15 09:06

Gigitsu


People also ask

How does sbt define dependencies?

Most of the time, you can simply list your dependencies in the setting libraryDependencies . It's also possible to write a Maven POM file or Ivy configuration file to externally configure your dependencies, and have sbt use those external configuration files.

What is Ivy in sbt?

Apache Ivy is a transitive package manager. It is a sub-project of the Apache Ant project, with which Ivy works to resolve project dependencies. An external XML file defines project dependencies and lists the resources necessary to build a project.

Does sbt use Ivy?

By default, sbt uses the standard Ivy home directory location ${user. home}/. ivy2/ . This can be configured machine-wide, for use by both the sbt launcher and by projects, by setting the system property sbt.

What is provided in sbt?

The sbt-assembly Plugin In that case, we can mark that dependency as “provided” in our build. sbt file. The “provided” keyword indicates that the dependency is provided by the runtime, so there's no need to include it in the JAR file.


3 Answers

See Appending to previous values: += and ++=:

Assignment with :=is the simplest transformation, but keys have other methods as well. If the T in SettingKey[T] is a sequence, i.e. the key’s value type is a sequence, you can append to the sequence rather than replacing it.

  • += will append a single element to the sequence.
  • ++= will concatenate another sequence.

For example, the key sourceDirectories in Compile has a Seq[File] as its value.

like image 104
Eugene Yokota Avatar answered Oct 22 '22 21:10

Eugene Yokota


It is += dep or ++= Seq(dep, dep2, dep3): "Of course, you can also use ++= to add a list of (read: multiple) dependencies all at once".

See collections.Seq for the + ("append an item") and ++ ("append a sequence") operators.

like image 26
user2864740 Avatar answered Oct 22 '22 22:10

user2864740


One way to strip this down to a simpler problem is to look at the two parts in separation.

First, there's the ...= part. You're probably familiar with this syntax from Java and other languages, but it performs the ... operator on both the left and right operands, and stores it back into the left operand - in this case, it stores it back into libraryDependencies.

Second, there's the choice of either ++ or +. If you take a look at the Seq Scaladoc then you'll find these two operators. The difference here is that:

  1. + takes a single element on the left-hand side, and prepends it to a sequence, returning a new sequence as the result.
  2. ++ takes a sequence on the left and a sequence on the right and returns a new sequence of both.

In practice, this means that you will get different results from the two.

  • List(1, 2, 3) ++ List(4, 5, 6) will return List(1, 2, 3, 4, 5, 6), but...
  • List(1, 2, 3) + List(4, 5, 6) will return List(List(1, 2, 3), 4, 5, 6).
like image 40
Ben Cox Avatar answered Oct 22 '22 21:10

Ben Cox