除了在JVM上与动态语言集成之外,还有像Scala这样的静态类型语言中Dynamic类型的其他强大用途是什么?
我发现这个益智游戏很有意思,特别是这段代码:
val (i, j): (Int, Int) = ("3", "4")
Run Code Online (Sandbox Code Playgroud)
在Scala 2.9.1中在运行时失败,但在编译时失败了w/2.10 M3(这很棒).我尝试跟踪新Scala版本中的内容,但我无法在此处连接点.什么改进导致了这种更精确的行为?
例如:
scala> val l:List[String] = List("one", "two")
l: List[String] = List(one, two)
scala> l.contains(1) //wish this didn't compile
res11: Boolean = false
Run Code Online (Sandbox Code Playgroud)
在Java中以这种方式完成事情的原因的各种解释在这里似乎并不适用,因为Map和Set确实实现了类型安全版本contains和朋友.有没有办法contains在Seq上做一个类型安全的东西,不能把它克隆成一个Set?
结构类型是其中之一"哇,很酷!" Scala的功能.但是,对于每个例子,我都可以想到它们可能会有什么帮助,隐式转换和动态混合组合通常看起来更好.它们的一些常见用途和/或何时适当的建议?
在Play入门文档中,他们显示了此控制器:
public static void index() {
Post frontPost = Post.find("order by postedAt desc").first();
List<Post> olderPosts = Post.find("order by postedAt desc").from(1).fetch(10);
render(frontPost, olderPosts);
}
Run Code Online (Sandbox Code Playgroud)
然后,在模板中使用frontPost和olderPosts而没有任何特殊的映射!
<a href="#">${frontPost.title}</a>
Run Code Online (Sandbox Code Playgroud)
Play怎么保留这些名字?
我已成功使用sbt-eclipse 3.0.0一段时间了.我最近升级到Play 2.3.8,需要升级到sbt-eclipse 4.0.0以避免这个问题.我尝试更新project/plugins.sbt,但在运行时遇到此错误activator eclipse:
java.lang.NoSuchMethodError: com.typesafe.sbteclipse.core.EclipsePlugin$.EclipseKeys()Lcom/typesafe/sbteclipse/core/EclipsePlugin$EclipseKeys$;
at play.PlayEclipse$class.eclipseCommandSettings(PlayEclipse.scala:93)
at play.Play$.eclipseCommandSettings(Project.scala:17)
at play.PlayScala$.projectSettings(Project.scala:72)
at sbt.Load$$anonfun$autoPluginSettings$1$1.apply(Load.scala:666)
at sbt.Load$$anonfun$autoPluginSettings$1$1.apply(Load.scala:666)
.....
Run Code Online (Sandbox Code Playgroud)
我尝试仅在我的〜/ .sbt/0.13/plugins/plugins.sbt中引用4.0.0,然后仅在我的project/plugins.sbt中引用,但这似乎没有区别.事实上,当两个引用被删除时,我能够运行3.0.0激活器eclipse .我不知道Activator/sbt在这种情况下如何找到3.0.0插件 - 也许我的错误是由于加载3.0.0和4.0.0?
环境:
Play 2.3.8
Activator 1.3.4
Scala 2.10.4
Run Code Online (Sandbox Code Playgroud)
〜/ .sbt/0.13/plugins/plugins.sbt为空
项目/ build.properties:
sbt.version=0.13.8
Run Code Online (Sandbox Code Playgroud)
项目/ plugins.sbt:
// Comment to get more information during initialization
logLevel := Level.Info
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.8")
addSbtPlugin("com.typesafe.sbt" % "sbt-uglify" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-uglify" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-gzip" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" …Run Code Online (Sandbox Code Playgroud) Fantom支持可证明不可变的类.知道一个类是不可变的编译器的优点必须是众多的,其中最重要的是保证在actor之间传递不可变的消息.Fantom的方法似乎很简单 - 它对Scala有什么困难?
我想将单个值转换为多个"特征"的集合,而不使用可变数据结构来收集值.我喜欢这样的幻想构造,它使用模式匹配,但在第一次匹配后不会停止:
scala> 2 multimatch {
case i if i > 0 => "Positive"
case i if i < 0 => "Negative"
case i if (i % 2 == 0) => "Even"
//yadda yadda
}
res0: Seq[java.lang.String] = List(Positive, Even)
Run Code Online (Sandbox Code Playgroud) 环顾网络上的示例,我猜测getStatus()成功返回零,并且大多数失败将表现为异常,而不是非零错误代码。
真的吗?当 getStatus() 返回非零值时抛出错误是否安全/正确?getStatus() 可能返回哪些非零值,这些值意味着什么?
假设我有一个拥有单个字段的actor.每100封给一个演员的消息中有99个读取该值,而第100个消息更新该值.在这种情况下,我想并行处理读取.换句话说,如何使用Actors实现读/写锁的性能?这与Scala标准演员或Akka一样实用吗?或者我错过了演员的观点:)
更新:修复了令人困惑的语言,抱歉
升级到 Play 2.6 后,我在日志中看到了很多“EntityStreamException: Entity stream truncation”。它看起来与客户端超时密切相关,这并不令人担忧。对于这种常见的 HTTP 事件,在应用程序代码中出现异常冒泡有点令人担忧,所以我担心这不仅仅是客户端超时。
akka.http.scaladsl.model.EntityStreamException: Entity stream truncation
at akka.http.impl.engine.parsing.HttpMessageParser$$anonfun$1.applyOrElse(HttpMessageParser.scala:321)
at akka.http.impl.engine.parsing.HttpMessageParser$$anonfun$1.applyOrElse(HttpMessageParser.scala:319)
at akka.stream.impl.fusing.Collect$$anon$6.$anonfun$wrappedPf$1(Ops.scala:217)
at akka.stream.impl.fusing.SupervisedGraphStageLogic.withSupervision(Ops.scala:178)
at akka.stream.impl.fusing.Collect$$anon$6.onPush(Ops.scala:219)
at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:515)
at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:478)
at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:374)
at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:588)
at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:474)
at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:563)
at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:730)
at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:745)
at akka.actor.Actor.aroundReceive(Actor.scala:517)
at akka.actor.Actor.aroundReceive$(Actor.scala:515)
at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:655)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:592)
at akka.actor.ActorCell.invoke(ActorCell.scala:561)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:258)
at akka.dispatch.Mailbox.run(Mailbox.scala:225)
at akka.dispatch.Mailbox.exec(Mailbox.scala:235)
at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Run Code Online (Sandbox Code Playgroud) 我想为scala.Enumeration添加一个方法.我的第一个方法是试图扩展它,但被这个咬了.我的第二种方法是尝试定义一个方法,并传入Enumeration - 如果有效,我希望使用隐式转换.但是,我很难用类型的返回类型保留类型.
object EnumExample {
object SampleEnum extends Enumeration {
val include, exclude = Value
}
def parse[T <: Enumeration](name:String, enum:T):T#Value =
enum.valueOf(name) match {
case Some(x) => x
case x => throw new RuntimeException("No field named '" + name + "' found on enum " + enum + ", legal values = " + enum.values)
}
def main(args:Array[String]) = {
//compiles fine, and preserves custom type
val withNameExample:SampleEnum.Value = SampleEnum.withName("include")
//also fine, but we lost type …Run Code Online (Sandbox Code Playgroud) scala ×9
actor ×1
akka ×1
akka-http ×1
eclipse ×1
enumeration ×1
sbteclipse ×1
scala-2.10 ×1
solr ×1
solrj ×1
types ×1