Scala 2.13.1 Unable to use macros - scala

I am unable to use macros in scala 2.13.1.
I keep getting the following error:
object blackbox is not a member of package scala.reflect.macros
This obviously isn't true, because scala 2.13.1 actually has these blackbox.
What am I not doing right?
Hello.scala
package example.core
import example.macros.MacroLibrary
object Hello extends App {
MacroLibrary.hello()
}
MacroLibrary.scala
package example.macros
// This line throws errors
import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.blackbox
object MacroLibrary {
def hello_impl(c: blackbox.Context)(): c.Expr[Unit] = {
import c.universe._
c.Expr(q"""println("Hello World")""")
}
def hello(): Unit = macro hello_impl
}
build.sbt
scalaVersion := "2.13.1"
Error
object blackbox is not a member of package scala.reflect.macros

They are not in the standard library, but in separate scala-reflect, which needs to be added as a dependency in build.sbt:
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
You can see that from Context documentation having "Scala Reflection Library" in the top left.

Related

How to use jquery-ui with JSImport

I want to access the jquery ui library in my scala js project. I have tried defining the following main module:
import org.scalajs.jquery.JQueryStatic
import scala.scalajs.js
import org.scalajs.dom
import scalatags.JsDom.all._
import scala.scalajs.js.annotation.JSImport
#JSImport("jquery", JSImport.Namespace)
#js.native
object JQuery extends JQueryStatic
#js.native
trait JQueryUI extends JQueryStatic {
def spinner(options: js.Object = js.Dynamic.literal()): JQueryUI = js.native
}
#JSImport("jquery-ui", JSImport.Namespace)
#js.native
object JQueryUI extends JQueryUI
object App {
def main(args: Array[String]): Unit = {
dom.document.getElementById("root").appendChild(div(input(id := "input")).render)
JQuery("#input").asInstanceOf[JQueryUI].spinner()
}
}
And my build.sbt is as follows:
enablePlugins(ScalaJSBundlerPlugin)
lazy val opexCounter = project.in(file(".")).settings(
name := "Repro",
scalaVersion := "2.12.8",
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.9.6",
"com.lihaoyi" %%% "scalatags" % "0.6.7",
"be.doeraene" %%% "scalajs-jquery" % "0.9.4"
),
npmDependencies in Compile ++= Seq(
"jquery" -> "2.2.1",
"jquery-ui" -> "1.12.1",
),
mainClass in Compile := Some("App"),
scalaJSUseMainModuleInitializer := true,
webpackDevServerPort := 3000
)
But when I load my page I get the following error in my console:
TypeError: qual$1.spinner is not a function
Is this not the correct way to import the library and if not what is?
The complete source for the project can be found here
I changed my npm dependency from jquery-ui to jquery-ui-bundle and imported it. I also had to make an explicit reference to my JQueryUIImport object in order to ensure it was instantiated. Those 2 changes fixed the problem

How to get the package of methods or objects in Scala?

How to get the package of methods or objects in Scala?
For example
import scala.math._
round().getClass // errors
Is there a way to get the desired output: "scala.math.round"? So that I can be sure of round() method doesn't come from any other package I've imported.
PS. Without using Intellij IDE. The IDE shows you this info, hence there has to be a function that IDE calls to get this info.
EDIT:
For example, running these commands in scala shell
With Scala reflection (libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.value in build.sbt) you can do
import scala.math._
import scala.reflect.runtime.universe._
object App {
def main(args: Array[String]): Unit = {
println(
showRaw(reify {
round(???)
})
)
}
}
Output at runtime will be
Expr(Apply(Select(Ident(scala.math.package), TermName("round")), List(Select(Ident(scala.Predef), TermName("$qmark$qmark$qmark")))))
Or you can add scalacOptions in Compile ++= Seq("-Xprint-types", "-Xprint:typer") to build.sbt.
Then compilation of
package pckg
import scala.math._
object App {
round(???)
}
will produce (at compile time)
Warning:scalac: package pckg{pckg.type} {
import scala.math._;
object App extends scala.AnyRef {
def <init>(): pckg.App.type = {
App.super{pckg.App.type}.<init>{()Object}(){Object};
(){Unit}
}{Unit};
scala.math.`package`.round{(x: Long)Long}(scala.Predef.???{Nothing}){Long}
}
}

ambiguous implicit values: match expected type cats.derived.MkShow[A]: show cats:kittens

I'm trying to create Show Instance for my custom Config class.
The build.sbt file is -
name := "circe-demo"
version := "0.1"
scalaVersion := "2.11.12"
resolvers += Resolver.bintrayRepo("ovotech", "maven")
libraryDependencies += "io.circe" %% "circe-core" % "0.11.0"
libraryDependencies += "io.circe" %% "circe-parser" % "0.11.0"
libraryDependencies += "io.circe" %% "circe-generic" % "0.11.0"
libraryDependencies += "org.typelevel" %% "kittens" % "1.2.0"
libraryDependencies ++= Seq(
"is.cir" %% "ciris-cats",
"is.cir" %% "ciris-cats-effect",
"is.cir" %% "ciris-core",
"is.cir" %% "ciris-enumeratum",
"is.cir" %% "ciris-refined"
).map(_ % "0.12.1")
Complete code is -
import enumeratum.{Enum, EnumEntry}
sealed abstract class AppEnvironment extends EnumEntry
object AppEnvironment extends Enum[AppEnvironment] {
case object Local extends AppEnvironment
case object Testing extends AppEnvironment
case object Production extends AppEnvironment
override val values: Vector[AppEnvironment] =
findValues.toVector
}
import java.net.InetAddress
import scala.concurrent.duration.Duration
final case class ApiConfig(host: InetAddress, port: Int, apiKey: String, timeout: Duration)
import java.net.InetAddress
import cats.Show
import cats.derived.semi
import ciris.config.loader.AppEnvironment.{Local, Production, Testing}
import enumeratum.EnumEntry
import eu.timepit.refined.auto._
import eu.timepit.refined.types.string.NonEmptyString
import scala.concurrent.duration._
final case class Config(appName: NonEmptyString, environment: AppEnvironment, api: ApiConfig)
object Config {
implicit val showConfig: Show[Config] = {
implicit val showDuration: Show[Duration] =
Show.fromToString
implicit val showInetAddress: Show[InetAddress] =
Show.fromToString
implicit def showEnumEntry[E <: EnumEntry]: Show[E] =
Show.show(_.entryName)
// Show.show[Config](x => s"api = ${x.api} appName = ${x.appName} environment ${x.environment}")
semi.show
}
}
semi.show in the above code throws the below exception -
[error] /Users/rajkumar.natarajan/Documents/Coding/kafka_demo/circe-demo/src/main/scala/ciris/config/loader/Config.scala:32:5: ambiguous implicit values:
[error] both value emptyProductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.HNil]
[error] and method emptyCoproductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.CNil]
[error] match expected type cats.derived.MkShow[A]
[error] show
[error] ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error]
I'm new to functional programming using cats.
How can I resolve this exception.
Unfortunately error reporting when such complicated implicits and macros are involved is far from perfect. The message you see actually means that some required implicits for the real generator (MkShow.genericDerivedShowProduct in this case) have not been found and the search went back to some where basic stuff where there is an ambiguity. And the stuff that is missing is mostly very basic such as an implicit for Show[Int] or Show[String]. The simplest way to get them all is to import cats.implicits._ but that will also bring catsStdShowForDuration which is a Show[Duration]. But since it's implementation is really the same as your custom one, it is easier to remove your custom one. One more thing that is missing is Show[NonEmptyString] and it is easy to create one
implicit def showNonEmptyString: Show[NonEmptyString] = Show.show(nes => nes)
To sum up, when I define your showConfig as
implicit val showConfig: Show[Config] = {
import cats.implicits._
// is already defined in cats.implicits._
//implicit val showDuration: Show[Duration] = Show.fromToString
implicit val showInetAddress: Show[InetAddress] = Show.fromToString
implicit def showEnumEntry[E <: EnumEntry]: Show[E] = Show.show(_.entryName)
implicit def showNonEmptyString: Show[NonEmptyString] = Show.show(nes => nes)
// Show.show[Config](x => s"api = ${x.api} appName = ${x.appName} environment ${x.environment}")
semi.show
}
it compiles for me.
P.S. is there any good reason why you put your AppEnvironment under ciris.* package? I'd say that generally putting your custom code into packages of 3-rd party library is an easy way to mess things up.

Macros annotation not working in IntelliJ14

I am having trouble using Scala Macros. It keeps on telling me to
enable macro paradise to expand macro annotations
from the #compileTimeOnly message I wrote. I followed all the instructions from Macro annotation documentation and the SBT example.
IDE: IntelliJ 14.1
Scala version: 2.11.7
Build.scala under the Project folder:
import sbt._
import sbt.Keys._
object Build extends Build {
val paradiseVersion = "2.1.0-M5"
lazy val sm = Project(id = "server-modules", base = file(".")).settings(
version := "1.0",
logLevel := Level.Warn,
scalacOptions ++= Seq(),
scalaVersion := "2.11.7",
crossScalaVersions := Seq("2.10.2", "2.10.3", "2.10.4", "2.10.5", "2.11.0", "2.11.1", "2.11.2", "2.11.3", "2.11.4", "2.11.5", "2.11.6", "2.11.7"),
resolvers += Resolver.sonatypeRepo("snapshots"),
resolvers += Resolver.sonatypeRepo("releases"),
addCompilerPlugin("org.scalamacros" % "paradise" % paradiseVersion cross CrossVersion.full),
libraryDependencies <+= (scalaVersion)("org.scala-lang" % "scala-reflect" % _),
)
}
Code:
#compileTimeOnly("enable macro paradise to expand macro annotations")
class dmCompile extends StaticAnnotation{
def macroTransform(annottees: Any*): Any = macro DMCompile.impl
}
object DMCompile {
def impl(c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
Log.info("Work work work!")
c.Expr(q"""var x = y""")
}
}
#dmCompile class Test{}
What exactly am I missing?
This took me the entire day, but I got it working.
Simply disregard the SBT settings for macros paradise and manually add it in the Preference -> Scala Compiler
That's it!
For me a solution was to change Incrementality type from IDEA to SBT in the Settings. This allows to use native SBT's build engine instead of IDEA's one.

SBT incorrect order of compilation

TL/DR: Checkout last two commits and run sbt clean compile: https://github.com/mirelon/akka-in-action/tree/json/chapter2
There is RestInterface.scala:
package com.goticks
import akka.actor._
import spray.routing._
import spray.http.StatusCodes
import spray.httpx.SprayJsonSupport._
import spray.routing.RequestContext
import akka.util.Timeout
import scala.concurrent.duration._
import scala.language.postfixOps
class RestInterface extends HttpServiceActor
with RestApi {
def receive = runRoute(routes)
}
trait RestApi extends HttpService { actor: Actor =>
import context.dispatcher
import com.goticks.TicketProtocol._
implicit val timeout = Timeout(10 seconds)
import akka.pattern.ask
def routes: Route =
path("events") {
get { requestContext =>
context.actorOf(Props[Resu]).ask(GetEvents)
}
}
}
class Responder(requestContext:RequestContext) extends Actor {
import TicketProtocol._
def receive = {
case Events(events) =>
requestContext.complete(StatusCodes.OK, events)
self ! PoisonPill
}
}
and Resu.scala (note that Resu is alphabetically after RestInterface)
package com.goticks
import akka.actor.Actor
class Resu extends Actor {
import TicketProtocol._
import spray.json._
def receive = {
case GetEvents => {
println(Event(event = "E").toJson)
}
}
}
object TicketProtocol {
import spray.json._
case class Event(event:String)
case object GetEvents
case class Events(events:List[Event])
object Event extends DefaultJsonProtocol {
implicit val format = jsonFormat1(Event.apply)
}
}
build.sbt:
name := "goticks"
version := "0.1-SNAPSHOT"
organization := "com.goticks"
scalaVersion := "2.11.1"
libraryDependencies ++= {
val akkaVersion = "2.3.4"
val sprayVersion = "1.3.1"
Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"io.spray" %% "spray-can" % sprayVersion,
"io.spray" %% "spray-routing" % sprayVersion,
"io.spray" %% "spray-json" % "1.2.6"
)
}
build.properties;
sbt.version=0.13.7
plugins.sbt:
resolvers += Classpaths.typesafeResolver
resolvers += "sbt-idea" at "http://mpeltonen.github.com/maven/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.10.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8")
Compilation issue
When I run sbt clean compile, everything is ok. But when I refactor the Resu class into Ress (note that Ress is alphabetically before RestInterface) (also rename its file, the diff can be viewed here: https://github.com/mirelon/akka-in-action/commit/583ca801fb7d1564024eee2f98d57f03ecacc6e5), then there is a compilation error:
[error] /home/miso/IdeaProjects/akka-in-action/chapter2/src/main/scala/com/goticks/Ress.scala:12: Cannot find JsonWriter or JsonFormat type class for com.goticks.TicketProtocol.Event
[error] println(Event(event = "E").toJson)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
Clearly there is an implicit json writer missing. Could it be due to incorrect order of compiling? Could sbt compile the classes in alphabetical order, ignoring imports?
Yes, sbt always compiles in alphabetical order. The issue is sbt has no idea the dependencies between files until it runs the compiler.
What you're seeing is the scala compiler itself is dependent on the ordering of source files. Sbt always sorts source files so you can at least work around these issues by placing code in an ordering that works.
I'm not 100% sure why you're hitting such an issue from some implicits + macro hackery, but that could be wrong.