scala-2.11

How to find the name of the enclosing source file in Scala 2.11

ε祈祈猫儿з 提交于 2019-12-06 03:09:15
问题 At compile time, how to retrieve the name of the current source file (where the code is written) in scala 2.11? 回答1: In the REPL, the name is console, but this shows that a position knows its source. scala> import scala.language.experimental.macros import scala.language.experimental.macros scala> import scala.reflect.macros.whitebox.Context import scala.reflect.macros.whitebox.Context scala> def f(c: Context): c.Tree = { import c._,universe._ ; Literal(Constant(c.enclosingPosition.source.file

canEqual() in the scala.Equals trait

余生颓废 提交于 2019-12-04 23:53:09
From the source code scala/Equals.scala ( here ): package scala trait Equals extends scala.Any { def canEqual(that: scala.Any): scala.Boolean def equals(that: scala.Any): scala.Boolean } In the documentation, it says: A method that should be called from every well-designed equals method that is open to be overridden in a subclass. I randomly picked a class which extends scala.Equals and which is simple enough to understand. I picked scala.Tuple2[+T1, +T2] , which extends the trait scala.Product[T1, T2] , which in turn extends the trait scala.Product , which in turn extends the trait scala

How to get the annotations of a method in Scala 2.11

左心房为你撑大大i 提交于 2019-12-04 21:05:44
Let's assume a controller object like this: object Users extends Controller { ... @ApiOperation( httpMethod = "POST", nickname = "authenticate", value = "Authenticates an user", notes = "Returns the JSON Web Token to be used in any subsequent request", response = classOf[models.auth.api.Jwt]) def authenticate = SecuredAction[Users.type]("authenticate").async(parse.json) { implicit request => ... } ... } How do I get the annotation values of the authenticate method at runtime? I've tried this: def methodAnnotations[T: TypeTag]: Map[String, Map[String, Map[String, JavaArgument]]] = { typeTag[T]

scala pattern match a function - how to get around type erasure

我是研究僧i 提交于 2019-12-04 11:18:55
I would like to pattern match a function, the problem is type erasure. Notice how in the snippet below, despite the warning issued a match occurs and a "wrong" one at that. scala> def f1 = ()=>true f1: () => Boolean scala> val fl = f1 fl: () => Boolean = <function0> scala> scala> fl match { | case fp :Function0[Boolean] => 1 | case _ => 2 | } res8: Int = 1 scala> scala> fl match { | case fp :Function0[String] => 1 | case _ => 2 | } <console>:11: warning: fruitless type test: a value of type () => Boolean cannot also be a () => String (but still might match its erasure) case fp :Function0

How to find the name of the enclosing source file in Scala 2.11

為{幸葍}努か 提交于 2019-12-04 08:41:10
At compile time, how to retrieve the name of the current source file (where the code is written) in scala 2.11? In the REPL, the name is console, but this shows that a position knows its source. scala> import scala.language.experimental.macros import scala.language.experimental.macros scala> import scala.reflect.macros.whitebox.Context import scala.reflect.macros.whitebox.Context scala> def f(c: Context): c.Tree = { import c._,universe._ ; Literal(Constant(c.enclosingPosition.source.file.name)) } f: (c: scala.reflect.macros.whitebox.Context)c.Tree scala> def g: String = macro f defined term

Scala instantiate objects from String classname

こ雲淡風輕ζ 提交于 2019-12-04 08:24:01
I have a trait, Action, that many different classes, ${whatever}Action, extend. I'd like to make the class that is in charge of instantiating these Action objects dynamic in the sense that it will not know which of the extending objects it will be building until it is passed a String with the name. I want it to take the name of the class, then build the object based on that String. I'm having trouble finding a concise/recent answer regarding this simple bit of reflection. I was hoping to get some suggestions as to either a place to look, or a slick way of doing this. You can use reflections as

How to override an implicit value?

空扰寡人 提交于 2019-12-03 16:09:43
Suppose I have the code: class A(implicit s:String = "foo"){println(s)} object X { implicit val s1 = "hello" } object Y { import X._ // do something with X implicit val s2 = "hi" val a = new A } I get the error: <console>:14: error: ambiguous implicit values: both value s2 in object Y of type => String and value s1 in object X of type => String match expected type String val a = new A Is there any way I can tell Scala to use the value s2 in Y ? (if I rename s2 to s1 , it works as expected but that is not what I want). Another solution is to not do import X._ , again something I'm trying to

How to fix the Dropping Close since the SSL connection is already closing error in spray

Deadly 提交于 2019-12-03 10:26:37
I’m making a call to an API, but most of the time I keep getting an error: “ Dropping Close since the SSL connection is already closing ” and “ Premature connection close (the server doesn't appear to support request pipelining) .” Like 90% of the time I get that error, meaning: on very rare occasions the query does return the data it supposed to. To make sure this wasn’t the API’s server issue, I replicate the same query using Node.js (Express and Request libs) and it works every time. It makes me almost sure is a spray bug . Here's an example of the code : case class MyClass(user: String,

Why doesn't the Def.inputTask macro work in Scala 2.11.1?

喜欢而已 提交于 2019-12-01 02:09:22
I'm using Scala 2.11.1 and sbt 0.13.5. I have an sbt plugin that contains a helper function to create input tasks as follows (the implementation is stripped away as it's irrelevant to the problem): def register(name: String, description: String): Def.Setting[InputTask[Unit]] = { InputKey[Unit](name, description) <<= Def.inputTask { println("test") } } This function compiles and works just fine in Scala 2.10.4, however once I switch to 2.11.1 it fails with the following error: can't expand macros compiled by previous versions of Scala Is the Def.inputTask macro simply broken in Scala 2.11.1, or

How can I handle a > 22 column table with Slick using nested tuples or HLists?

眉间皱痕 提交于 2019-11-30 08:09:01
I'm new to Scala (using 2.10) and Slick (using 2.0-M2). I see that one of the ways to get around the 22 column limit for tables in Slick is to use nested tuples. I can't figure out how to do that, despite finding this partial code on GitHub . Current dev branch Scala (2.11-M5) supports case classes with more than 22 elements, but not tuples with arity > 22. And Slick is not yet distributed for Scala 2.11 pre-releases. How can I define a 33 column table (and have it work with all Slick's syntactic sugar)? N.B., I'm trying to support an existing schema and can't change the table normalization.