Use cases for different sbt Key operators

前端 未结 3 1575
小蘑菇
小蘑菇 2021-02-04 03:53

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 \"+=\", \"++=\", \"<+=\", \"<++=

3条回答
  •  余生分开走
    2021-02-04 04:50

    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 as compile -- 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 vals in Scala) while tasks are executed every time they're called (similar to defs 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 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.

    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.

提交回复
热议问题