Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is that .value can be called on a SettingKey or TaskKey?

Tags:

scala

sbt

One can write something like

(managedClasspath in Compile).value

to obtain the value of managedClasspath in the Compile configuration.

The type of (managedClasspath in Compile) is yet again a sbt.TaskKey (because we call the in method with a ConfigKey).

There is however no value method on SettingKey or TaskKey, and I can't find any implicit class that provides such a method. So how come it exists? Is this some magical macro voodoo?

like image 816
fommil Avatar asked Aug 11 '14 15:08

fommil


1 Answers

It's both, there are a few things at work components:

  1. In sbt, any *XYZKey[_] can be converted into an appropriate Initialize[_] instance via an implicit. This, by default, is an initializer that reads the existing value at the key and returns it.
  2. The sbt.std.MacroValue[T] type is a compile-time only class which holds something that can have .value called on it: http://www.scala-sbt.org/0.13.5/api/index.html#sbt.std.MacroValue. We use this to track the underlying instances in the macro and denote that they have special significance (i.e. we have to rework the code such that we wait for the value to exist before using it).
  3. The sbt.Def object has a set of implicits called macroValueXYZ which lift Initialize[_] instances into the macro API.

So, as you can see, it's a bit of black magic through our internals to get there. We'll have to look into a way to better document the API in a scaladoc tool.

like image 98
jsuereth Avatar answered Nov 15 '22 05:11

jsuereth