我正在使用一个需要使用事务初始化许多对象的系统,并且出于超出此问题范围的原因,这些事务必须传递给构造函数.像这样:
trait Mutable
class Txn(i: Int) {
def newID(implicit m: Mutable): Int = i
override def finalize(): Unit = println("Finalised " + i)
}
class User(t0: Txn) extends Mutable {
val id = t0.newID(this)
}
Run Code Online (Sandbox Code Playgroud)
现在我担心垃圾收集交易存在问题:
val u = new User(new Txn(1234))
System.gc() // hmmm, nothing seems to happen?
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:t0构造函数参数是否会被垃圾收集,或者我是否在这里创建内存泄漏?在等效的Java代码中,我想我会有这样的事情:
public class User implements Mutable {
final int id;
public User(Txn t0) {
id = t0.newID(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我肯定t0是收集的.但在Scala案件中这是真的吗?
如果没有,我怎样才能确保t0收集垃圾?请记住,我必须将事务作为构造函数参数传递,因为User该类实现了一些必须传递给 …
在Scala中实现仿射变换的最佳方法是什么?标准库或Spire中似乎没有任何内容.AWT AffineTransformation类非常可变,我绝对不想改变Graphics2D类.编写自己的函数或者在返回值函数中包装Java类是否更有效,或者是否已经有一个合适的Scala库?
编辑:我不认为基本方程对代码来说太具挑战性.复杂性似乎是为90/180/270轮换添加特殊情况并处理int/double/float转换以获得全面的解决方案.
我有问题Predef.any2stringadd,不幸的是,官方认为不是PITA.我改变了我的API
trait Foo {
def +(that: Foo): Foo
}
Run Code Online (Sandbox Code Playgroud)
类型类方法
object Foo {
implicit def fooOps(f: Foo): Ops = new Ops(f)
final class Ops(f: Foo) {
def +(that: Foo): Foo = ???
}
}
trait Foo
Run Code Online (Sandbox Code Playgroud)
现在我可以在编译代码中隐藏这个可怕的方法,如下所示:
import Predef.{any2stringadd => _}
Run Code Online (Sandbox Code Playgroud)
但是,这在我的REPL/interpreter环境中失败了.
val in = new IMain(settings, out)
in.addImports("Predef.{any2stringadd => _}") // has no effect?
Run Code Online (Sandbox Code Playgroud)
我怎么能告诉翻译人员这个烦人的方法呢?
就像这个问题的作者一样,我试图理解Scala 2.10期货和承诺中用户可见承诺的推理.
import scala.concurrent.{ future, promise }
val p = promise[T]
val f = p.future
val producer = future {
val r = produceSomething()
p success r
continueDoingSomethingUnrelated()
}
val consumer = future {
startDoingSomething()
f onSuccess {
case r => doSomethingWithResult()
}
}
Run Code Online (Sandbox Code Playgroud)
我想象的是调用produceSomething导致运行时异常的情况.因为承诺和生产者 - 未来是完全分离的,这意味着系统挂起,消费者永远不会成功或失败.
因此,使用promises的唯一安全方法需要类似的东西
val producer = future {
try {
val r.produceSomething()
p success r
} catch {
case e: Throwable =>
p failure e
throw …Run Code Online (Sandbox Code Playgroud) 遇到类型"种类"的问题:
trait Sys[ S <: Sys[S]]
trait Expr[S <: Sys[S], A]
trait Attr[S <: Sys[S], A[_]]
def test[ S <: Sys[S]]: Attr[S, ({type l[x<:Sys[x]]=Expr[x,Int]})#l] = ???
Run Code Online (Sandbox Code Playgroud)
这失败了
error: kinds of the type arguments (S,[x <: Sys[x]]Expr[x,Int]) do not conform
to the expected kinds of the type parameters (type S,type A) in trait Attr.
[x <: Sys[x]]Expr[x,Int]'s type parameters do not match type A's expected parameters:
type x's bounds <: Sys[x] are stricter than type _'s declared bounds >: Nothing <: …Run Code Online (Sandbox Code Playgroud) types scala existential-type type-kinds partially-applied-type
我对 sbt 和 Typesafe 存储库有一个奇怪的问题:
[info] Resolving com.typesafe.play#play-json_2.10;2.2.0 ...
[warn] module not found: com.typesafe.play#play-json_2.10;2.2.0
[warn] ==== local: tried
[warn] /Users/hhrutz/.ivy2/local/com.typesafe.play/play-json_2.10/2.2.0/ivys/ivy.xml
[warn] ==== public: tried
[warn] http://repo1.maven.org/maven2/com/typesafe/play/play-json_2.10/2.2.0/play-json_2.10-2.2.0.pom
[warn] ==== Typesafe Releases: tried
[warn] https://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-json_2.10/2.2.0/play-json_2.10-2.2.0.pom
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: com.typesafe.play#play-json_2.10;2.2.0: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
sbt.ResolveException: unresolved dependency: com.typesafe.play#play-json_2.10;2.2.0: not found
Run Code Online (Sandbox Code Playgroud)
我可以从http://repo.typesafe.com/typesafe/releases/com/typesafe/play/play-json_2.10/2.2.0/手动下载 .pom 和 .jar,所以服务器肯定在那里。
这可能是sbt问题吗?如果它肯定有正确的 URL,它为什么会失败的任何线索?
如何确保最新的测试不能并行运行?在0.12之前,我有一个sbt设置:
parallelExecution in Test := false
Run Code Online (Sandbox Code Playgroud)
较新的版本引入了一些复杂的机制.这个更简单,旧的方式仍然适用于0.13吗?
在测试期间,我不希望任何东西并行运行.
在以下代码段中,
trait MyType1; trait MyType2
import scala.concurrent.Promise
val p1 = Promise[Option[MyType1]]()
val p2 = Promise[MyType2]()
Run Code Online (Sandbox Code Playgroud)
我将p1和p2传递给另一个函数,在那里我使用一个成功的Future来完成Promise.调用此函数后,我尝试读取Promise中的值:
trait Test {
// get the Future from the promise
val f1 = p1.future
val f2 = p2.future
for {
someF1Elem <- f1
f1Elem <- someF1Elem
f2Elem <- f1Elem
} yield {
// do something with f1Elem and f2Elem
"..."
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我遇到了一些编译器问题.
Error:(52, 19) type mismatch;
found : Option[Nothing]
required: scala.concurrent.Future[?]
flElem <- someF1Elem
^
Run Code Online (Sandbox Code Playgroud)
IntelliJ没有显示任何错误,也没有显示任何错误,并且类型看起来是对齐的.但我不确定编译器为什么不开心!有线索吗?
是否存在类型安全的等于===Scala的实现,其开销为零==?也就是说,与===Scalaz和ScalaUtils 不同,这是一个使用直接宏来执行检查的实现?
我想===在很多地方使用,但这些都是热点,所以我不希望这会产生任何额外的运行时成本(比如构造类型等).
在 IntelliJ IDEA 中,是否有键盘快捷键或导航快捷键可以在 Scala 伴侣object和伴侣之间跳转class?