我想知道如何使用多种类型模式匹配.我有:
abstract class MyAbstract
case class MyFirst extends MyAbstract
case class MySecond extends MyAbstract
case class MyThird extends MyAbstract // shouldn't be matched and shouldn't call doSomething()
val x: MyAbstract = MyFirst
x match { 
 case a: MyFirst => doSomething()
 case b: MySecond => doSomething()
 case _ => doSomethingElse()
}
所以我想写一些类似的东西:
x match {
 case a @ (MyFirst | MySecond) => doSomething()
 case _ => doSomethingElse()
}
我在一些教程中看到了类似的构造,但它给了我错误:
pattern type is incompatible with expected type;
[error]  found   : object MyFirst …我正在寻找Scala的Redis客户端实现.使用Akka期货,客户端应该是异步和非阻塞的.我发现或多或少有用的东西:
但他们两个都有他们的问题.第一个使用旧版本的Akka,这会导致编译问题,第二个使用scala.actors.Futures.future而不是Akka期货.我看到几个月前发生的一次对话:groups.google.com/forum/#!msg/akka-user/EDKA2aTigho/_wWcNIz2O3wJ
但我没有找到任何解决方案.有人有类似的东西?
谢谢你的回答.
我有一个模型,它有一些Option字段,其中包含另一个Option字段.例如:
case class First(second: Option[Second], name: Option[String])
case class Second(third: Option[Third], title: Option[String])
case class Third(numberOfSmth: Option[Int])
我从外部JSON收到这些数据,有时这些数据可能包含null,这就是这种模型设计的原因.
所以问题是:获得最深领域的最佳方法是什么?
First.get.second.get.third.get.numberOfSmth.get
上面的方法看起来很丑陋,如果其中一个对象是None,可能会导致异常.我正在寻找Scalaz lib,但没有找到更好的方法来做到这一点.
有任何想法吗?提前致谢.
简而言之:是否可以使用akka-http重新加载静态资源?
多一点:
App对象启动我的Main
课程。  getFromResourceDirectory用来查找我的资源文件夹。我想拥有的是在开发过程中热交换我的静态资源。例如,我有index.html或application.js,需要更改,并且在刷新浏览器而不重启服务器后想查看更改。做这种事情的最佳实践是什么?
我知道Play!可以这样做,但是我不想Play!仅仅因为这个而基于我的项目。
我正在寻找循环一段时间的可能性.例如,我想println("你好!")5分钟.
我正在使用Scala和Akka.
我正在考虑使用未来,它将在5分钟内完成,同时我将使用while循环,并检查它是否未完成.这种方法对我不起作用,因为我的班级不是演员,我不能从循环外完成未来.
任何想法,或者可能有这样的事情的现成解决方案?
目前难看的解决方案:
    def now = Calendar.getInstance.getTime.getTime
    val ms = durationInMins * 60 * 1000
    val finish = now + ms
    while (now <= finish) {
       println("hi")
    }
提前致谢!
我在Play中发现了有趣的东西!框架形式验证.例如,我有这样的形式:
case class Foo(mystring: String, myint: Int, mybool: Boolean) { // doing cool stuff here }
val myForm = Form(
    mapping(
      "mystring" -> text,
      "myint" -> number,
      "mybool" -> boolean
)(Foo.apply)(Foo.unapply))
当我在我的Json中绑定没有"mybool"的数据时,验证会传递并创建一个"mybool = false"的对象.这是非常奇怪的行为,好像我将传递相同的数据,但没有"mystring"字段,我会得到Validation Errors: Map(mystring -> error.required)我期望看到的 - 因为字段丢失.
如果我使布尔字段可选,但我手动添加这样的检查:
"mybool" -> optional(boolean).verifying("mybool.required", _.isDefined)
并且没有字段绑定数据我得到了预期的错误:
Validation Errors: Map(mybool -> mybool.required)
示例数据集:
{
  "mystring": "stringHere",
  "myint": 33
}
为什么需要检查不适用于布尔值?它的最佳解决方法是什么?这是一场比赛!我或者只是不明白的东西?
谢谢你的回答.
我有一个大对象:
case class BigObject(val str: String, val number: Int) {
val someVal = ...
val someVal2 = ...
    }
我想复制这个对象而不重新评估值.可能吗?现在我正在使用这种方法:
val newBigObject = oldBigObject.copy(str = newStr)
正如我从日志/调试器中看到的那样,"someVal"和"someVal2"被重新评估.有可能避免它吗?由于我的BigObject非常庞大,价值重估需要一些时间,但性能对我来说非常重要.
谢谢你的回答!
我正试图在我的应用程序中覆盖Akka配置.我为应用程序创建了额外的lib,它还有application.conf文件,因为它使用了Akka.所以我有两个:
application.conf in my lib: 
my-conf {
 something = 1
}
application.conf in my app, which uses the lib:
something-else = "foo"
my-conf {
 something = 1000
}
当我从Intellij Idea运行应用程序时,一切都很好,并且正在覆盖lib配置.要在我的应用程序中加载配置,我正在使用简单的ConfigFactory.load()操作.但是,当我创建一个我的应用程序的jar mvn clean compile assembly:single并尝试使用此命令运行它时:java -Xmx4048m -XX:MaxPermSize=512M -Xss256K -classpath myApp.jar com.myapp.example.MyMain我收到错误:
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'something-else'
所以我决定在我的应用程序中重命名conf文件,并以这种方式加载它:
  val defConfig = ConfigFactory load
  val myConfig = ConfigFactory load "myconf"
  val combined = myConfig.withFallback(defConfig)
  val config = ConfigFactory load combined
它发现缺少设置,但遗憾的是我的应用程序中的配置不会覆盖我的lib中的配置.在我的lib中,我以默认方式加载配置:val settings …
我有dateTimeField和ListView的表单.ListView看起来像这样:
final ListView<String> countryView = new ListView<String>("country", model.<List<String>>bind("country")) {
            @Override
            protected void populateItem(final ListItem<String> item) {
                    final String country = item.getModelObject();
                    item.add(new ValidationDisplayableLabel("country", country, new String[] { modelPath }));
                    item.add(new AjaxLink("deleteLink") {
                        @Override
                        public void onClick(AjaxRequestTarget target) {
                            model.getObject().getCountry().remove(country);
                            if (issPeriod) {
                                addButton.setVisible(true);
                                countryTextField.setVisible(true);
                                findButton.setVisible(true);
                            }
                            if (target != null)
                                target.addComponent(rowPanel);
                        }
                    });
            }
        };
        countryTextField = new ValidationDisplayableTextField("countryCodeInput", model.bind("oneCountry"), "job.country.value");
        **countryView.setReuseItems(true);**
        rowPanel.add(countryView);
        rowPanel.add(countryTextField);
        addButton.setOutputMarkupPlaceholderTag(true);
        rowPanel.add(addButton);
addButton看起来像这样:
AjaxSubmitLink addButton = new AjaxSubmitLink(LinkNames.addCountry.toString()) {
        @Override
        public void onSubmit(AjaxRequestTarget target, Form form) …我正在寻找一种方法,为一个案例类使用两种不同的形式.我试图用额外的构造函数做到这一点,但失败了.看一下代码片段:
case class LoginDetails(password: String, field3: Option[Int], field4: String)
case class User(username: String, loginDetails: LoginDetails) {
   def this(username: String, password: String, field3: Option[Int], field4: String) = this(username, LoginDetails(password, field3, field4))
// some logic inside
    }
val loginDetailsForm = Form(
 mapping(
   "password" -> text,
   "field3" -> optional(number),
   "field4" -> text
 )(LoginDetails.apply)(LoginDetails.unapply))
val oldForm = Form(
 mapping(
   "username" -> email,
   "password" -> text,
   "field3" -> optional(number),
   "field4" -> text
 )(User.apply)(User.unapply))
val newForm = Form(
     mapping(
       "username" -> email,
       "loginDetails" -> loginDetailsForm.mapping
     )(User.apply)(User.unapply)) …我已经将自定义验证添加到我的页面,并且在我点击UI上的"保存"按钮后,从业务逻辑层调用此验证AjaxSubmitLink.
在我的页面上,我有apache wicket DateTimeField,但它的验证无法正常工作:错误消息没有出现FeedbackPanel,这是在页面上添加的,我的自定义验证正确显示在那里.
所以例如我用"321"填充小时字段,我将在控制台中出错:
警告org.apache.wicket.protocol.http.WebSession - 组件目标反馈消息未被删除.这可能是因为您在页面上缺少FeedbackPanel.消息:[FeedbackMessage message ="找不到语言[en]的密钥[hours.RangeValidator]的翻译!",reporter = hours,level = ERROR]
也许有人有类似的问题,并有解决方案吗?
谢谢!