scala-2.10

Calling a constructor through reflection in scala 2.10

坚强是说给别人听的谎言 提交于 2019-12-24 01:27:08
问题 What's the best practice for calling a constructor of a class in scala 2.10 (M4+) ? 回答1: Answering my own question: Calling the constructor is different than invoking a method. Here's the right way to do it in scala 2.10 import reflect.runtime.universe._ import reflect.runtime.currentMirror val typ = typeOf[Range] val constructor = typ.members.find(_.kind == "constructor").get.asMethodSymbol currentMirror reflectClass typ.typeSymbol.asClassSymbol reflectConstructor constructor apply (1,10,1)

Do macros make naturally chained comparisons possible in Scala?

混江龙づ霸主 提交于 2019-12-24 00:26:00
问题 Scala does not provide chained comparisons as Python does: // Python: 0 < x <= 3 // Scala: 0 < x && x <= 3 Will Scala 2.10 with the new macro feature enable the programmer write a library that adds this feature? Or is this beyond the scope of Scala's macros? Macros seem to be the right choice for the implementation of such syntactic sugar as they do not complicate the parser/compiler. 回答1: You don't need macros for it: class ChainedComparisons[T : Ordering](val res: Boolean, right: T) { def <

Retrieve the name of the value a Scala macro invocation will be assigned to

一世执手 提交于 2019-12-24 00:21:26
问题 I'm attempting to write a macro that would wrap a function and deducting a parameter from the value its invocation will be assigned to. object TestMacros { def foo(name: String): String = name.toUpper def bar = macro barImpl def barImpl(c: Context): c.Expr[String] = { import c.universe._ //TODO extract value name (should be baz) c.Expr[String](Apply( Select(newTermName("TestMacros"), newTermName("foo")), // Probably wrong, just typed it quickly for demonstration purposes List(Literal(Constant

Access code file and line number from Scala macro?

让人想犯罪 __ 提交于 2019-12-23 17:20:57
问题 How can I access the name of the code file and line number in a Scala macro? I looked at SIP-19 and it says it can be easily implemented using macros... EDIT: To clarify, I want the code file and line number of the caller. I already have a debug macro and I want to modify it to print the line number and file name of whoever calls debug 回答1: You want c.macroApplication.pos , where c is for Context . c.enclosingPosition finds the nearest macro on the stack that has a position. (See the other

simple scala macro

℡╲_俬逩灬. 提交于 2019-12-23 17:19:58
问题 I would like to have a scala macro that does the following: When I write: myCreateCityMacro("paris") myCreateCityMacro("vallorbe") I would like to get: val paris = new City("paris") val vallorbe = new City("vallorbe") 回答1: This can be solved using scala Dynamic feature: import scala.language.dynamics object Cities extends App { var c = new DynamicMap[String, City]() createCity("Paris") createCity("Vallorbe") println(c.Paris, c.Vallorbe) def createCity(name: String) { c.self.update(name, new

Scala check type of generics

本秂侑毒 提交于 2019-12-22 10:08:02
问题 How do I do something like this in Scala? case class Foo[A](x: A) { def get[T]: Option[T] = x match { case x: T => Some(x) // if x is of type T i.e. T =:= A case _ => None } } val test = Foo("hi") assert(test.get[Int] == None) assert(test.get[String] == Some("hi")) I tried this and ran into some weird time inference failure: import scala.util.{Try, Success} import reflect._ case class Foo[A](x: A) extends Dynamic { def get[T: ClassTag]: Option[T] = Try(x.asInstanceOf[T]) match { case Success

Get filename of the current file in scala

旧街凉风 提交于 2019-12-22 08:46:22
问题 Is there a way the file name of the current file (when the code is written) in scala? Like my class is in a file like com/mysite/app/myclass.scala and i want to call a method that will return "myclass.scala" (or the full path...) Thank you! 回答1: This can be achieved with Scala macros, an experimental language feature available from version 2.10. Macros make it possible to interact with the building of the AST during the source code parsing phase, and to modify trees of AST before the actual

Strange error with higher-kinded types in scala 2.10.0 (works with scala 2.9.2)

可紊 提交于 2019-12-22 06:28:58
问题 This code compiles with Scala 2.9.2: trait HK { type Rep[A] def unzip1[A, B, C[_]](ps: Rep[C[(A, B)]]): (Rep[C[A]], Rep[C[B]]) def doUnzip1[A, B](ps: Rep[List[(A, B)]]) = unzip1(ps) } But with Scala 2.10.0 it doesn't compile with the following error (higherKinded language feature is enabled): [info] Compiling 1 Scala source to /home/klyuchnikov/code/hk/target/scala-2.10/classes... [error] /home/klyuchnikov/code/hk/src/main/scala/HK.scala:6: type mismatch; [error] found : HK.this.Rep[List[(A,

Type equality in the Scala 2.10 Reflection API

二次信任 提交于 2019-12-22 05:53:46
问题 I'm running into a weird issue with reflection in Scala 2.10.0 Milestone 4 that I can't wrap my head around. First for the stuff that works the way I'd expect: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> trait A[X]; trait B[Y] extends A[Y] defined trait A defined trait B scala> typeOf[B[String]].parents res0: List[reflect.runtime.universe.Type] = List(java.lang.Object, A[String]) scala> typeOf[B[String]].parents contains typeOf[A[String]] res1

No multipartconfig for servlet error from Jetty using scalatra

佐手、 提交于 2019-12-22 04:07:46
问题 I am trying to unit test an upload call but I get this error for the following code: @MultipartConfig(maxFileSize = 3145728) class WebServlet extends ScalatraServlet with FileUploadSupport { override def isSizeConstraintException(e: Exception) = e match { case se: ServletException if se.getMessage.contains("exceeds max filesize") || se.getMessage.startsWith("Request exceeds maxRequestSize") => true case _ => false } error { case e: SizeConstraintExceededException => RequestEntityTooLarge("too