我正在实现一个Swing组件,我想要克服#### untypedness Reactor.所以我认为这会奏效:
trait Foo[A] extends scala.swing.Publisher {
final case class Bar(parent: Vector[A], children: A*) extends scala.swing.event.Event
}
trait Test {
val foo: Foo[Int]
foo.reactions += {
case foo.Bar(parent, children) => {
println(parent.sum - children)
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这给了我两个编译器警告:
The outer reference in this type test cannot be checked at run time.
final case class Bar(parent: Vector[A], children: A*) extends scala.swing.event.Event
^
The outer reference in this type test cannot be checked at run time.
case foo.Bar(parent, children) => …Run Code Online (Sandbox Code Playgroud) 我正在使用JSON扩展,它依赖于Mandubian的play-json 2.2-SNAPSHOT.一切正常,直到现在我有一个基于Scala-STM的项目.sbt报告以下问题:
[error] Modules were resolved with conflicting cross-version suffixes
in {file:folder}project:
[error] org.scala-stm:scala-stm _2.10, _2.10.0
java.lang.RuntimeException: Conflicting cross-version suffixes in:
org.scala-stm:scala-stm
Run Code Online (Sandbox Code Playgroud)
有没有机会深入研究这两个"冲突"版本的来源?我很惊讶play-json应该依赖于scala-stm ?!
此外,有没有办法说服sbt关闭.......因为显然2.10和2.10.0是等效版本.
编辑:这似乎是一个0.13错误(可能与Play-JSON无关),因为如果我恢复到0.12.4,项目成功更新和构建.我仍然对sbt 0.13的工作感兴趣.
我正在尝试使用GraphViz重新创建二叉搜索树的示例图.这是它最终应该看起来的样子:

这是我的第一次尝试:
digraph G {
nodesep=0.3;
ranksep=0.2;
margin=0.1;
node [shape=circle];
edge [arrowsize=0.8];
6 -> 4;
6 -> 11;
4 -> 2;
4 -> 5;
2 -> 1;
2 -> 3;
11 -> 8;
11 -> 14;
8 -> 7;
8 -> 10;
10 -> 9;
14 -> 13;
14 -> 16;
13 -> 12;
16 -> 15;
16 -> 17;
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是GraphViz并不关心树的水平位置,所以我得到:

如何添加约束以使顶点的水平位置反映其总排序?
什么是以下类型:
(Int, => Double) => String
Run Code Online (Sandbox Code Playgroud)
请注意后面的尾随逗号Int.显然它不是一个句法漏洞,而是一些不同的东西
(Int => Double) => String
Run Code Online (Sandbox Code Playgroud)
例如,当使用重载时:
trait Foo {
def bar(x: (Int, => Double) => String): Unit
def bar(x: (Int => Double) => String): Unit
}
Run Code Online (Sandbox Code Playgroud) 我想为sbt项目添加一个依赖项,它只用于编译.它既不应该在运行时类路径上,也不应该在已发布的POM中以任何形式显示.
我们的想法是添加一个仅存根库(OrangeExtensions),以便可以在任何平台上编译项目,而不仅仅是OS X.
有可能这样吗:
libraryDependencies += "com.yuvimasory" % "orange-extensions" % "1.3.0" % ???
Run Code Online (Sandbox Code Playgroud)
?
我想sbt update在我的源代码中调用,以更新多个sbt项目.在shell中这很容易:
cd /path/to/project && sbt update
Run Code Online (Sandbox Code Playgroud)
但是,如果我scala.sys.process在我的代码中使用它,它将不记得cd因此sbt在错误的目录中调用.像这样的代码:
import scala.sys.process._
("cd /path/to/project" #&& "sbt update").!!
Run Code Online (Sandbox Code Playgroud)
我没有在文档中找到通过控制台设置sbt的项目路径的任何可能性.如果像这样的东西工作会很好:
"sbt -projectPath /path/to/project update".!!
Run Code Online (Sandbox Code Playgroud)
如果这样的事情是可能的,这将为我节省很多麻烦!(特别是它在UNIX和Windows上运行.)
在Scala中是否可以编写如下内容:
trait Road {
...
}
class BridgeCauseway extends Road {
// implements method in Road
}
class Bridge extends Road {
val roadway = new BridgeCauseway()
// delegate all Bridge methods to the `roadway` member
}
Run Code Online (Sandbox Code Playgroud)
或者我是否需要逐个实现每个Road方法,并调用相应的方法roadway?
我在一些单元测试代码中发现了一些令人困惑的特性使用,例如:
trait MyTrait {
val t1 = ... //some expression
val t2 = ... //some expression
}
Run Code Online (Sandbox Code Playgroud)
然后使用new实例化特征,同时在实例化之后用花括号包裹一些表达式.
test("it is a test") {
new MyTrait {
// do something with t1 and t2
}
}
Run Code Online (Sandbox Code Playgroud)
我对这种奇怪的语法感到困惑.
我的问题是:
为什么使用花括号跟随特征实例化?
在这种情况下特征实例化的目的是什么,其他情况也可能有帮助?
我可以将一个带隐式参数的方法转换为函数吗?
trait Tx
def foo(bar: Any)(implicit tx: Tx) {}
foo _ // error: could not find implicit value for parameter tx: Tx
Run Code Online (Sandbox Code Playgroud)
我试图实现以下,最好是我可以以某种方式使它与普通调用一起工作withSelection(deleteObjects):
trait Test {
def atomic[A](fun: Tx => A): A
def selection: Iterable[Any]
def withSelection(fun: Iterable[Any] => Tx => Unit) {
val sel = selection
if (sel.nonEmpty) atomic { implicit tx =>
fun(sel)(tx)
}
}
object deleteAction {
def apply() {
withSelection(deleteObjects) // !
}
}
def deleteObjects(xs: Iterable[Any])(implicit tx: Tx): Unit
}
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题,但就我所见,它并没有解决从方法到功能的问题.
看一下我的库的一些scala-docs,在我看来,值类中有一些不需要的噪音.例如:
implicit class RichInt(val i: Int) extends AnyVal {
def squared = i * i
}
Run Code Online (Sandbox Code Playgroud)
这引入了不必要的符号i:
4.i // arghh....
Run Code Online (Sandbox Code Playgroud)
那些东西出现在scala文档和IDE自动完成中,这实际上并不好.
那么......关于如何缓解这个问题的任何想法?我的意思是你可以使用,RichInt(val self: Int)但这不会让它变得更好(4.self,是吗?)
编辑:
在以下示例中,编译器是否擦除了中间对象?
import language.implicitConversions
object Definition {
trait IntOps extends Any { def squared: Int }
implicit private class IntOpsImpl(val i: Int) extends AnyVal with IntOps {
def squared = i * i
}
implicit def IntOps(i: Int): IntOps = new IntOpsImpl(i) // optimised or not?
}
object Application …Run Code Online (Sandbox Code Playgroud) scala ×9
sbt ×2
binary-tree ×1
currying ×1
dot ×1
graphviz ×1
implicit ×1
maven ×1
process ×1
proxy ×1
swing ×1
traits ×1
type-erasure ×1
types ×1
value-class ×1
visibility ×1