给定一个类的实例,我们显然可以返回它的名字:
trait MixedInClassDiscovery {
val className = this.getClass.getName
}
class AClass extends MixedInClassDiscovery {
...
this.className // returns "AClass"
...
}
Run Code Online (Sandbox Code Playgroud)
但这种方式使用反射,一次用于每个实例AClass.相反,每个班级都可以做一次吗?
我想到的一个解决方案是将它混合到伴随对象而不是类本身.
我有一个具有两个工作进程的主管:一个处理与远程服务器连接的TCP客户端和一个处理连接协议的FSM.
处理子进程中的TCP错误会使代码变得非常复杂.所以我更喜欢"让它崩溃",但这有一个不同的问题:当服务器无法访问时,将很快达到最大重启次数,并且主管将与我的整个应用程序一起崩溃,这对于这个案例.
我想要的是采用后退的重启策略; 如果失败了,如果主管知道什么时候由于崩溃而重新启动(即将它作为参数传递给init函数)就足够了.我找到了这个邮件列表线程,但是有更官方/更好的测试解决方案吗?
是否可以写一个更快的等效函数?
prepend(X, Tuple) ->
list_to_tuple([X | tuple_to_list(Tuple)]).
Run Code Online (Sandbox Code Playgroud) 当拥有生成端口的进程死亡并由主管重新启动时会发生什么?
有没有办法让旧港口不与其所有者一起死亡,让新主人"接管"?
如果失败了,是否有可能确保当端口死亡时生成的进程终止?
是否可以检查活动 SQLite 连接的线程模式?我知道sqlite3_threadsafe(),但是
sqlite3_threadsafe() 函数的返回值仅显示线程安全的编译时设置,而不是 sqlite3_config() 对该设置所做的任何运行时更改。
编译此规范时:
import org.specs.Specification
import org.specs.matcher.extension.ParserMatchers
class ParserSpec extends Specification with ParserMatchers {
type Elem = Char
"Vaadin DSL parser" should {
"parse attributes in parentheses" in {
DslParser.attributes must(
succeedOn(stringReader("""(attr1="val1")""")).
withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String"))))
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ParserSpec.scala:21
error: scala is not an enclosing class
withResult(Map[String, AttrVal]("attr1" -> AttrVal("val1", "String"))))
^
Run Code Online (Sandbox Code Playgroud)
我根本不理解这里的错误信息.它为什么会出现?
Scala版本是2.8.1,规格版本是1.6.7.2.
DslParser.attributes有类型Parser[Map[String, AttrVal]]和组合器succeedOn,withResult定义如下:
trait ParserMatchers extends Parsers with Matchers {
case class SucceedOn[T](str: Input,
resultMatcherOpt: Option[Matcher[T]]) extends Matcher[Parser[T]] { …Run Code Online (Sandbox Code Playgroud) 我有一个异类List,如下所示:
val l = List(1, "One", true)
Run Code Online (Sandbox Code Playgroud)
我需要通过仅提取属于给定类的对象来过滤其对象.为此我写了一个非常简单的方法:
def filterByClass[A](l: List[_], c: Class[A]) =
l filter (_.asInstanceOf[AnyRef].getClass() == c)
Run Code Online (Sandbox Code Playgroud)
请注意,我有义务将显式转换添加到AnyRef以避免此编译问题:
error: type mismatch;
found : _$1 where type _$1
required: ?{val getClass(): ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method any2stringadd in object Predef of type (x: Any)scala.runtime.StringAdd
and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
are possible conversion functions from _$1 to ?{val getClass(): ?}
l filter (_.getClass() == c)
Run Code Online (Sandbox Code Playgroud)
但是通过这种方式调用: …
是否有专门支持编辑Blueprint XML文件的Eclipse插件?例如自动完成参考ID,接口名称等.
经典的强类型MVP模式在Scala中看起来像这样:
trait IView { }
trait Presenter[View <: IView] { // or have it as an abstract type member
val view : View
}
case class View1(...) extends IView { ... }
case object Presenter1 extends Presenter[View1] {
val view = View1(...)
}
Run Code Online (Sandbox Code Playgroud)
现在,我想知道是否有任何改善它的好方法,我错过了...
scala ×4
erlang ×3
.net ×1
benchmarking ×1
eclipse ×1
erlang-otp ×1
erlang-ports ×1
mvp ×1
performance ×1
reflection ×1
specs ×1
sqlite ×1
testing ×1
traits ×1