所以我在函数上有一个注释(DefDef).此注释具有参数.但是,我对如何从构造函数中获取参数感到困惑.
用法示例:
class TestMacro {
@Foo(true)
def foo(): String = ""
foo
}
Run Code Online (Sandbox Code Playgroud)
这是注释的代码:
class Foo(b: Boolean) extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro Foo.impl
}
object Foo {
def impl(c: whitebox.Context)(annottees: c.Tree*): c.Expr[Any] = {
import c.universe._
//how do I get value of `b` here???
c.abort(c.enclosingPosition, "message")
}
}
Run Code Online (Sandbox Code Playgroud) 这是一个简单的重现器,我在其中定义了一个带有隐式重新排序转换的"可交换"对类型.如果函数的参数f位于预先存在的命名值(t在示例中),则编译器会按预期应用隐式转换.但是,如果我尝试f直接调用literal CommutativePair,它会因类型错误而失败.在这种情况下,编译器不应用隐式重新排序转换.
object repro {
import scala.language.implicitConversions
case class CommutativePair[A, B](a: A, b: B)
object CommutativePair {
// Support a kind of commutative behavior via an implicit reordering
implicit def reorderPair[B, A](pair: CommutativePair[B, A]) =
CommutativePair(pair.b, pair.a)
}
// The idea is to allow a call to 'f' with Pair[Int, String] as well,
// via implicit reorder.
def f(p: CommutativePair[String, Int]) = p.toString
val t = CommutativePair(3, "c")
// This works: the implicit …Run Code Online (Sandbox Code Playgroud) 我开始并运行了一个 Scala 进程。
val dir = "/path/to/working/dir/"
val stockfish = Process(Seq("wine", dir + "stockfish_8_x32.exe"))
val logger = ProcessLogger(printf("Stdout: %s%n", _))
val stockfishProcess = stockfish.run(logger, connectInput = true)
Run Code Online (Sandbox Code Playgroud)
该进程读取和写入标准 IO(控制台)。如果进程已经启动,如何向进程发送字符串命令?
Scala 进程 API 具有 ProcessBuilder,它又具有一堆有用的方法。但是 ProcessBuilder 是在进程开始编写复杂的 shell 命令之前使用的。Scala 也有 ProcessIO 来处理输入或输出。我也不需要。我只需要向我的进程发送消息。
在 Java 中,我会做这样的事情。
String dir = "/path/to/working/dir/";
ProcessBuilder builder = new ProcessBuilder("wine", dir + "stockfish_8_x32.exe");
Process process = builder.start();
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new …Run Code Online (Sandbox Code Playgroud) 如果你像我一样,你偶尔想要为Scala集合或序列编写增强的方法,但是你想要绑定集合类型以及元素类型,而不仅仅是向Seq [T]进行upcast.
scala implicit implicit-conversion higher-kinded-types scala-collections
fabric8 kubernetes Java 和 Scala 客户端 API 非常适合与 kubernetes(或 OpenShift)对话,但其文档非常稀少。向在 Kubernetes pod 中运行的容器添加资源需求的代码示例是什么?
scala ×5
implicit ×2
annotations ×1
client ×1
compiler-bug ×1
fabric8 ×1
implicits ×1
inputstream ×1
io ×1
java ×1
kubernetes ×1
outputstream ×1
process ×1
scala-macros ×1