已经一个方法被提出来处理的重载方法双定义是,以取代与模式匹配超载:
object Bar {
def foo(xs: Any*) = xs foreach {
case _:String => println("str")
case _:Int => println("int")
case _ => throw new UglyRuntimeException()
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法要求我们放弃对参数的静态类型检查foo.能够写作会好得多
object Bar {
def foo(xs: (String or Int)*) = xs foreach {
case _: String => println("str")
case _: Int => println("int")
}
}
Run Code Online (Sandbox Code Playgroud)
我可以接近Either,但它有两种以上的快速变得难看:
type or[L,R] = Either[L,R]
implicit def l2Or[L,R](l: L): L or R = Left(l)
implicit def r2Or[L,R](r: R): L or R = …Run Code Online (Sandbox Code Playgroud) 背景
正如在这个问题中所指出的,我正在使用Scalaz 7迭代器来处理恒定堆空间中的大型(即无界)数据流.
我的代码看起来像这样:
type ErrorOrT[M[+_], A] = EitherT[M, Throwable, A]
type ErrorOr[A] = ErrorOrT[IO, A]
def processChunk(c: Chunk, idx: Long): Result
def process(data: EnumeratorT[Chunk, ErrorOr]): IterateeT[Vector[(Chunk, Long)], ErrorOr, Vector[Result]] =
Iteratee.fold[Vector[(Chunk, Long)], ErrorOr, Vector[Result]](Nil) { (rs, vs) =>
rs ++ vs map {
case (c, i) => processChunk(c, i)
}
} &= (data.zipWithIndex mapE Iteratee.group(P))
Run Code Online (Sandbox Code Playgroud)
问题
我似乎遇到了内存泄漏,但我对Scalaz/FP不太熟悉,不知道这个bug是在Scalaz中还是在我的代码中.直观地说,我希望这段代码只需要(大约为)P倍的Chunk-size空间.
注:我发现了一个类似的问题,其中OutOfMemoryError遇到,但我的代码没有使用consume.
测试
我跑了一些测试试图找出问题.总而言之,只有在使用zipWithIndex和泄漏时才会出现泄漏group.
// no zipping/grouping …Run Code Online (Sandbox Code Playgroud) 我的方法定义如下所示
def processLine(tokens: Array[String]) = tokens match { // ...
Run Code Online (Sandbox Code Playgroud)
假设我想知道第二个字符串是否为空白
case "" == tokens(1) => println("empty")
Run Code Online (Sandbox Code Playgroud)
不编译.我该怎么做呢?
最近我一直很烦,因为我达到了C++(甚至0x)感觉非常有限的程度,所以我开始寻找替代方案.
忘记Java,C#,Python或Ruby.我仍然喜欢C++的低级特性,我不喜欢虚拟机.此外,我是一个游戏引擎开发,所以我必须要发展核心程序必须是非常快,而且最近我已经饿了码表现.C++ 对我来说几乎是一种语言,但是关于如何使用模板有很多例外,而GCC并没有像我希望的那样优化内容.
所以我正在考虑开始学习D.
你认为它足以满足我作为游戏开发者的需求吗?我很谨慎,因为我从来没有听说过D被用于那个.
谢谢!
我想知道scala是否有相当于java的@SuppressWarnings可以应用于函数或任何忽略函数发出的任何弃用警告[1]?
1:在我的情况下的相关警告是:method stop in class Thread is deprecated: see corresponding Javadoc for more information.我知道停止的问题,但仍有一些情况由于遗留代码我们必须使用它.
我正在尝试模拟一个带有call-by-name参数的方法调用:
import org.scalatest.WordSpec
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
trait Collaborator {
def doSomething(t: => Thing)
}
trait Thing
@RunWith(classOf[JUnitRunner])
class Test extends WordSpec with MockitoSugar {
"The subject under test" should {
"call the collaborator" in {
// setup
val m = mock[Collaborator]
val t = mock[Thing]
// test code: this would actually be invoked by the SUT
m.doSomething(t)
// verify the call
verify(m).doSomething(t)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我主要对Mockito感兴趣,因为我正在使用它,但我有兴趣看看是否有任何主要的模拟框架能够进行这种测试.测试在运行时在线上失败,verify出现类似的错误
Run Code Online (Sandbox Code Playgroud)Argument(s) are different! Wanted: collaborator.doSomething( ($anonfun$apply$3) <function> …
我有一个自定义异常类,如下所示:
case class CustomException(errorMsg:String) extends Exception(error:String)
Run Code Online (Sandbox Code Playgroud)
当我捕获异常时,我需要的是抛出我的自定义异常并将我的错误消息传递给自定义异常.我期望从CustomException构造函数调用super(errMsg)但是,这不是现在的情况,我得到了一个编译错误.
catch {
case s: Exception => throw CustomException("This is a custom message")
}
Run Code Online (Sandbox Code Playgroud)
我怎么能调用超级构造函数:
super(errorMessage)
Run Code Online (Sandbox Code Playgroud) 我是否有可能通过scala doc在全局范围内搜索类,特征或对象中的方法?示例:我有一个方法名称,我不知道它属于哪个类,所以我可以在一些scaladoc搜索中键入它,它将显示包含具有给定名称的方法的类.(PS:我会添加标签scaladoc,但显然我不配......)
我最近看到了几个Scala问题(例如这里,这里和这里),它们要求使用代理,并且它在我自己的工作中不止一次出现.Scala库有许多代理特征(14,如果我计算正确的话).
代理类/特征通常包含许多样板:
class FooProxy(val self: Foo) extends Foo {
// added behavior
def mymethod = ...
// forwarding methods
def method1 = self.method1
def method2(arg: String) = self.method2(arg)
...
}
trait Foo {
def method1: Unit
def method2(arg: String): Unit
}
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是定义一个Proxy[T]可以如下使用的特征:
class FooProxy(val self: Foo) extends Proxy[Foo] {
// added behavior
def mymethod = ...
}
Run Code Online (Sandbox Code Playgroud)
哪里trait Proxy[T] extends T.当然,如果Proxy没有编译器魔法,实际上不可能定义特征.
我的下一个想法是寻找一个编译器插件(这种功能显然不在现有的编译器中,或者这14个代理特性的来源会小得多).果然,我找到了Kevin Wright的AutoProxy插件.该插件旨在整齐地解决代理问题,以及其他用例(包括动态混合):
class FooProxy(@proxy val …Run Code Online (Sandbox Code Playgroud) 有没有人知道一个库或者至少有一些关于在Java中创建和使用持久数据结构的研究?我没有将持久性称为长期存储,而是将持久性称为不变性(参见维基百科条目).
我正在探索为持久性结构建模api的不同方法.使用构建器似乎是一个有趣的解决方案:
// create persistent instance
Person p = Builder.create(Person.class)
.withName("Joe")
.withAddress(Builder.create(Address.class)
.withCity("paris")
.build())
.build();
// change persistent instance, i.e. create a new one
Person p2 = Builder.update(p).withName("Jack");
Person p3 = Builder.update(p)
.withAddress(Builder.update(p.address())
.withCity("Berlin")
.build)
.build();
Run Code Online (Sandbox Code Playgroud)
但这仍然有点像锅炉板.有任何想法吗?