I would like to write an annotation macro that adds an extends <sometype> to traits where <sometype> can be specified at compile time.
How can a compile time parameter be passed to a macro expansion? Ideally I would like to specify a command line argument at the compiler call.
Macro annotations don't have access to the command-line flags passed to scalac. However, one possible way to achieve this might be to use system properties.
For example, in the macro annotation implementation
// MyMacro.scala
val someType = sys.props.getOrElse("myapp.sometype", ???)
Then pass the type as a command line option
// command-line
scalac -Dmyapp.sometype=foobar Code.scala
Similarly, it's possible to run sbt -Dsometype=foobar compile. However, note that the JVM process needs to start with the system property flag, so setting scalacOptions += "-Dsometype=foobar" may not work.
Related
I have a scala compiler 2.12.11 and compiler prints some warnings, like:
private default argument in class SomeClass is never used
while as it is used.
I have seen a Scala 2.12.2 emits a ton of useless "Warning: parameter value ... in method ... is never used" warnings. How to get rid of them? however it doesn't help me, as it is not possible to negate params.
Can you help me to surpress this warning, however saving another unused warnings? Currently, I have -Xlint option.
Instead of -Xlint which is a batch, you can turn on specific options manually. For instant in 2.13 I can print available options like this:
scalac -Xlint:help
Enable recommended warnings
adapted-args An argument list was modified to match the receiver.
nullary-unit `def f: Unit` looks like an accessor; add parens to look side-effecting.
inaccessible Warn about inaccessible types in method signatures.
infer-any A type argument was inferred as Any.
missing-interpolator A string literal appears to be missing an interpolator id.
doc-detached When running scaladoc, warn if a doc comment is discarded.
private-shadow A private field (or class parameter) shadows a superclass field.
type-parameter-shadow A local type parameter shadows a type already in scope.
poly-implicit-overload Parameterized overloaded implicit methods are not visible as view bounds.
option-implicit Option.apply used an implicit view.
delayedinit-select Selecting member of DelayedInit.
package-object-classes Class or object defined in package object.
stars-align In a pattern, a sequence wildcard `_*` should match all of a repeated parameter.
strict-unsealed-patmat Pattern match on an unsealed class without a catch-all.
constant Evaluation of a constant arithmetic expression resulted in an error.
unused Enable -Wunused:imports,privates,locals,implicits,nowarn.
nonlocal-return A return statement used an exception for flow control.
implicit-not-found Check #implicitNotFound and #implicitAmbiguous messages.
serial #SerialVersionUID on traits and non-serializable classes.
valpattern Enable pattern checks in val definitions.
eta-zero Usage `f` of parameterless `def f()` resulted in eta-expansion, not empty application `f()`.
eta-sam The Java-defined target interface for eta-expansion was not annotated #FunctionalInterface.
deprecation Enable -deprecation and also check #deprecated annotations.
byname-implicit Block adapted by implicit with by-name parameter.
recurse-with-default Recursive call used default argument.
unit-special Warn for specialization of Unit in parameter position.
multiarg-infix Infix operator was defined or used with multiarg operand.
implicit-recursion Implicit resolves to an enclosing definition.
Default: All choices are enabled by default.
so I could e.g. enable -Xlint:inaccessible -Xlint:adapted-args -Wunused:privates,locales or whatever I want instead of everything. For 2.12 this list would be different. (You can also check scalac -X, scalac -Y and scalac -W).
An alternative is to enable warnings and suppress them when you "breaking" something consciously. For unused you have #scala.annotation.unused, for other warnings in 2.12 and before there is silencer plugin and since 2.13 there is #scala.annotation.nowarn annotation.
The error message
`value` can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting.
val x = version.value
^
clearly indicates how to fix the problem, for example, using :=
val x = settingKey[String]("")
x := version.value
The explanation in sbt uses macros heavily states
The value method itself is in fact a macro, one that if you invoke it
outside of the context of another macro, will result in a compile time
error, the exact error message being...
And you can see why, since sbt settings are entirely declarative, you
can’t access the value of a task from the key, it doesn’t make sense
to do that.
however I am confused what is meant by declarative nature of sbt being the reason. For example, intuitively I would think the following vanilla Scala snippet is semantically similar to sbt's
def version: String = ???
lazy val x = s"Hello $version" // ok
trait Foo {
def version: String
val x = version // ok
}
As this is legal, clearly the Scala snippet is not semantically equivalent to the sbt one. I was wondering if someone could elaborate on why value cannot be used outside macros? Is the reason purely syntactic related to macro syntax or am I missing something fundamental about sbt's nature?
As another sentence there says
Defining sbt’s task engine is done by giving sbt a series of settings, each setting declaring a task implementation. sbt then executes those settings in order. Tasks can be declared multiple times by multiple settings, the last one to execute wins.
So at the moment when the line
val x = version.value
would be executed (if it were allowed!), that whole program is still being set up and SBT doesn't know the final definition of version.
In what sense is the program "still being set up"?
SBT's order of actions is, basically (maybe missing something):
All your Scala build code is run.
It contains some settings and tasks definitions, SBT collects those when it encounters (along with ones from core, plugins, etc.)
They are topologically sorted into the task graph, deduplicated ("the last one to execute wins"), etc.
Settings are evaluated.
Now you are allowed to actually run tasks (e.g. from SBT console).
version.value is only available after step 4, but val x = version.value runs on step 1.
Would not lazy evaluation take care of that?
Well, when you write val x = ... there is no lazy evaluation. But lazy val x = ... runs on step 1 too.
We have problems when using Scalac -Xfatal-warnings in the following cases:
Implicit vals used by macros internally
Internal vals that macros auto-generate
In both cases, we see Scalac failing to compile because it detects some values are not used, while we know they are (simply, when we remove them, the code doesn't compile anymore)
Although the two might be symptoms of the same problem in Scalac, they boil down to the same issue for us: we need to disable the -Ywarn-unused in Scala 2.11.12
Is there a way to exclude specific class files so they won't be affected by the compiler flag?
As far as I know there is no way of disabling scalac flag for just one file (if you compile your whole project at once by e.g sbt). You can extract class into separate module with different compile flags.
In case of implicit vals used internally in macros, personally I use -Ywarn-macros:after flag, which make these implicits used in macro count as used. (Talking about Scala 2.12.4).
In Scala 2.12 is there any difference between any of these options:
-Xlint:adapted-args vs -Ywarn-adapted-args
-Xlint:nullary-unit vs -Ywarn-nullary-unit
-Xlint:inaccessible vs -Ywarn-inaccessible
-Xlint:nullary-override vs -Ywarn-nullary-override
-Xlint:infer-any vs -Ywarn-infer-any
If not, which one makes more sense to use? I also compile with fatal-warnings, and almost all -Ywarn and -Xlint options.
If you run scalac -Xlint:help you'll see
❯ scalac -Xlint:help
Enable or disable specific warnings
adapted-args Warn if an argument list is modified to match the receiver.
nullary-unit Warn when nullary methods return Unit.
inaccessible Warn about inaccessible types in method signatures.
nullary-override Warn when non-nullary `def f()' overrides nullary `def f'.
infer-any Warn when a type argument is inferred to be `Any`.
missing-interpolator A string literal appears to be missing an interpolator id.
doc-detached A Scaladoc comment appears to be detached from its element.
private-shadow A private field (or class parameter) shadows a superclass field.
type-parameter-shadow A local type parameter shadows a type already in scope.
poly-implicit-overload Parameterized overloaded implicit methods are not visible as viewbounds.
option-implicit Option.apply used implicit view.
delayedinit-select Selecting member of DelayedInit.
by-name-right-associative By-name parameter of right associative operator.
package-object-classes Class or object defined in package object.
unsound-match Pattern match may not be typesafe.
stars-align Pattern sequence wildcard must align with sequence component.
constant Evaluation of a constant arithmetic expression results in an error.
unused Enable -Ywarn-unused:imports,privates,locals,implicits.
Default: All choices are enabled by default.
so there's no difference between selecting Xlint options or using the corresponding flags directly. I would just go with -Xlint (no options, so all enabled by default) and remove the ones I don't need, for example
-Xlint:-unused,_
to enable everything but the unused flag.
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?
It's both, there are a few things at work components:
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.
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).
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.