The documentation for sbt seems to be really lacking here, so I'd like to get a definitive answer on this: what is the difference between "+=", "++=", "<+=", "<++=", and "<<=" when operating on Keys?
You cannot find documentation, because as @JacekLaskowski correctly pointed out all operators except for +=
, ++=
and :=
are deprecated.
You can however find the Documentation if you switch to older version of sbt.
If you however are stuck to older version, this is their meaning (via documentation):
+=
and ++=
append to previous value, where first appends single element and next appends a Seq
~=
transforms value, e.g. you want to use value stored in a setting to get a new setting.<<=
depends on another key, for example if you call organization <<= name
, then organization
value is equal to name
value. You can depend on multiple values, e.g. organization <<= (name, version) { (n, v) => /* do something with values */ }
<+=
and <++=
are appending with dependencies, like the append, but you can use another's setting value to compute new valueSaid that, @JacekLaskowski is right, and if you are using sbt 13.x or greater you should not have to use those operators in favours of macros.
Quoting Task v. Setting keys:
A
TaskKey[T]
is said to define a task.sbt's map describing the project can keep around a fixed string value for a setting such as
name
, but it has to keep around some executable code for a task such ascompile
-- even if that executable code eventually returns a string, it has to be re-run every time.A given key always refers to either a task or a plain setting. That is, "taskiness" (whether to re-run each time) is a property of the key, not the value.
In other words, settings are immutable and initialized at build startup (similar to val
s in Scala) while tasks are executed every time they're called (similar to def
s in Scala).
Quoting Defining tasks and settings:
Using
:=
, you can assign a value to a setting and a computation to a task. For a setting, the value will be computed once at project load time. For a task, the computation will be re-run each time the task is executed.
Quoting Appending to previous values: += and ++=:
Assignment with
:=
is the simplest transformation, but keys have other methods as well. If theT
inSettingKey[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.
Wrapping it up, you should only be concerned with :=
(assignment macro), +=
(append macro) and ++=
(concatenation macro). The remaining ones, i.e. <<=
, <+=
and <++=
, are no longer recommended for common use cases.
As a matter of fact, all operations can be expressed with the simple assignment macro :=
(paraphrasing the upcoming book SBT in Action).
Are you really sure, the docs are "really lacking here"?! I doubt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With