可能重复:Scala
中:_*(冒号下划线明星)做了什么?
我正在使用REPL来调用带有Scala数组的Java vararg方法.
如果我这样做,我会收到错误:
case class Person(name: String, age: Int)
val array = Array(classOf[String], classOf[Int])
Person.getClass.getMethod("apply", array)
Run Code Online (Sandbox Code Playgroud)
但如果我这样做,那么它的工作原理:
Person.getClass.getMethod("apply", array:_*)
Run Code Online (Sandbox Code Playgroud)
我的问题是:_*做什么的?它在Scala API中的定义在哪里?
当我尝试同时使用 actix-web 3 和 rusoto 0.46 时,出现以下运行时错误:
thread 'actix-rt:worker:0' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /Users/matt/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/blocking/pool.rs:85:33
Run Code Online (Sandbox Code Playgroud)
小可复制:
use actix_web::{get, App, HttpResponse, HttpServer, Responder}; // 3
use rusoto_core::Region; // 0.46
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, ListTablesInput};
use std::default::Default;
#[get("/tables")]
async fn tables(_req_body: String) -> impl Responder {
let client = DynamoDbClient::new(Region::default());
let list_tables_input: ListTablesInput = Default::default();
match client.list_tables(list_tables_input).await {
Ok(_output) => HttpResponse::Ok().finish(),
Err(_error) => HttpResponse::InternalServerError().finish(),
}
}
#[actix_web::main]
async fn main() …Run Code Online (Sandbox Code Playgroud) 我正在尝试确定在编译时传递给宏的参数的类型.它似乎在我使用<:<时起作用,但在我使用时不起作用=:=.我不知道为什么.谁能指出我正确的方向?我在下面列出了示例代码.
这个宏:
import language.experimental.macros
import scala.reflect.macros.Context
object Macros {
def say(param: Any): Unit = macro impl
def impl(c: Context)(param: c.Expr[Any]): c.Expr[Unit] = {
if (param.actualType.<:<(c.universe.typeOf[String])) {
c.universe.reify { printf("string: %s\n", param.splice) }
} else if (param.actualType.<:<(c.universe.typeOf[Int])) {
c.universe.reify { printf("int: %d\n", param.splice) }
} else {
c.universe.reify { printf("any: %s\n", param.splice) }
}
}
}
Run Code Online (Sandbox Code Playgroud)
由此代码调用:
object Test extends App {
Macros.say("Hi")
Macros.say(1)
Macros.say(Blah)
}
case object Blah
Run Code Online (Sandbox Code Playgroud)
返回:
string: Hi
int: 1
any: Blah
Run Code Online (Sandbox Code Playgroud)
但是,如果我检查类型equality( …
我有以下宏:
package macros
import scala.reflect.macros.blackbox.Context
object CompileTimeAssertions {
def mustBeCaseClass[T]: Unit =
macro CompileTimeAssertionsImpl.mustBeCaseClass[T]
}
object CompileTimeAssertionsImpl {
def mustBeCaseClass[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = {
import c.universe._
val symbol = c.weakTypeTag[T].tpe.typeSymbol
if (!symbol.isClass || !symbol.asClass.isCaseClass) {
c.error(c.enclosingPosition, s"${symbol.fullName} must be a case class")
}
reify(Unit)
}
}
Run Code Online (Sandbox Code Playgroud)
它在不涉及泛型时有效,但在以下情况下失败:
import macros.CompileTimeAssertions._
import org.scalatest.{Matchers, WordSpec}
case class ACaseClass(foo: String, bar: String)
class NotACaseClass(baz: String)
class MacroSpec extends WordSpec with Matchers {
"the mustBeCaseClass macro" should {
"compile when passed a case class" …Run Code Online (Sandbox Code Playgroud) 以下代码段按预期返回true:
import scala.reflect.runtime.universe._
typeOf[Seq[Int]] <:< typeOf[Traversable[Int]]
Run Code Online (Sandbox Code Playgroud)
但是,这段代码不是:
val s = Seq[Int](1,2,3)
val m = runtimeMirror(this.getClass.getClassLoader)
val t = m.reflect(s).symbol.typeSignature
t <:< typeOf[Seq[Int]]
Run Code Online (Sandbox Code Playgroud)
我确信我只是遗漏了一些显而易见的东西,但我已经在REPL上工作了几个小时而且还没有解决.任何建议将不胜感激.提前致谢.
scala ×4
scala-macros ×2
actix-web ×1
java ×1
macros ×1
reflection ×1
rusoto ×1
rust ×1
scala-2.11 ×1