这个问题关系到这一个:是否有可能创建一组类的类(意思是它的扩展Set特性)Scala中,其中使用的平等来定义包含关系是由用户,而不是被定义的==?
测试这是否真的有效的一种方法是检查是否filter返回相同的集合类型.
// typeclass for equality
trait Equals[T] {
def isEqual(t1: T, t2: T): Boolean
}
// an object representing plane coordinates
case class Coordinate(i: Int, j: Int)
// an equality saying that 2 coordinates are equal if they are on
// the same horizontal line
implicit def horizontalEquality: Equals[Coordinate] = new Equals[Coordinate] {
def isEqual(t1: Coordinate, t2: Coordinate) = t1.i == t2.i
}
// we create an EqualitySet[T] where T must …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用宏实现自定义字符串插值方法,我需要一些关于使用API的指导.
这是我想要做的:
/** expected
* LocatedPieces(List(("\nHello ", Place("world"), Position()),
("\nHow are you, ", Name("Eric"), Position(...)))
*/
val locatedPieces: LocatedPieces =
s2"""
Hello $place
How are you, $name
"""
val place: Piece = Place("world")
val name: Piece = Name("Eric")
trait Piece
case class Place(p: String) extends Piece
case class Name(n: String) extends Piece
/** sequence of each interpolated Piece object with:
* the preceding text and its location
*/
case class LocatedPieces(located: Seq[(String, Piece, Position)])
implicit class s2pieces(sc: StringContext) {
def …Run Code Online (Sandbox Code Playgroud) 我在代码中发现了这种模式很多次:
if (doIt)
object.callAMethod
else
object
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种语法上更令人愉快的方式来编写上面的代码,特别是为了避免重复object变量.就像是:
// using the Scalaz "pipe" operator
// and "pimping" f: T => T with a `when` method
object |> (_.callAMethod).when(doIt)
Run Code Online (Sandbox Code Playgroud)
不幸的是,上面的行失败了,因为类型推断需要参数类型(_.callAMethod).
我现在最好的方法是:
implicit def doItOptionally[T](t: =>T) = new DoItOptionally(t)
class DoItOptionally[T](t: =>T) {
def ?>(f: T => T)(implicit doIt: Boolean = true) =
if (doIt) f(t) else t
}
implicit val doIt = true
object ?> (_.callAMethod)
Run Code Online (Sandbox Code Playgroud)
不是很好,因为我必须声明一个,implicit val但如果有几个链接的电话,这会得到回报:
object ?> (_.callAMethod) ?> (_.callAnotherMethod)
Run Code Online (Sandbox Code Playgroud)
有没有人有更好的主意?我在这里错过了一些Scalaz魔法吗?
我一直在使用Scala Macros并在宏中使用以下代码:
val fieldMemberType = fieldMember.typeSignatureIn(objectType) match {
case NullaryMethodType(tpe) => tpe
case _ => doesntCompile(s"$propertyName isn't a field, it must be another thing")
}
reify{
new TypeBuilder() {
type fieldType = fieldMemberType.type
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我已经成功了c.universe.Type fieldMemberType.这表示对象中某个字段的类型.一旦我得到了,我想TypeBuilder在reify中创建一个新对象.TypeBuilder是一个带抽象参数的抽象类.这个抽象参数是fieldType.我希望这fieldType是我之前找到的类型.
运行此处显示的代码会返回一个fieldMemberType not found.有什么方法可以让我fieldMemberType在reify子句中工作吗?
我有以下宏定义一个类并返回该类的实例(使用Scala 2.10.2和宏插件):
def test[T] = macro testImpl[T]
def testImpl[T : c.WeakTypeTag](c: Context): c.Expr[Any] = {
import c.universe._
val className = newTypeName("Test")
c.Expr { q"""
class $className {
def method = 1
}
new $className
"""}
}
Run Code Online (Sandbox Code Playgroud)
当我调用宏时:
case class Cat(name: String)
val t = test[Cat].method
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
method method in class Test cannot be accessed in Test
val t = test[Cat].method
^
Run Code Online (Sandbox Code Playgroud)
我的总体目标是使用吸血鬼方法并使用准引号来描述生成的类.我该如何解决这个错误?
在TestNg和Java中,我们可以使用DataProvider运行多个测试用例,这可以作为单独的测试运行,这意味着测试的执行不会在失败时停止.是否有ScalaTest或Specs/Specs2的模拟?
Scala 重写的specs2测试框架将自动化测试与scalacheck集成在一起.specs2文档中给出的关于如何将scalacheck与specs2一起使用的示例使用整数或更复杂的自定义生成器,如eric的json示例中所示.
虽然试图让一个稍微不那么复杂的例子工作,但我很难挣扎,因为我不知道如果我想生成String参数而不是Integers,会如何使用specs2和scalacheck.这个快速入门示例如何?
import org.scalacheck._
object StringSpecification extends Properties("String") {
property("startsWith") = Prop.forAll((a: String, b: String)
=> (a+b).startsWith(a))
property("endsWith") = Prop.forAll((a: String, b: String)
=> (a+b).endsWith(b))
// Is this really always true?
property("concat") = Prop.forAll((a: String, b: String) =>
(a+b).length > a.length && (a+b).length > b.length
)
property("substring") = Prop.forAll((a: String, b: String) =>
(a+b).substring(a.length) == b
)
property("substring") = Prop.forAll((a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
) …
Run Code Online (Sandbox Code Playgroud) 如何配置sbt 0.10以将junitxml选项与specs2配合使用?
该specs2文档说,这是用SBT 0.7.x来做到这一点:
override def testOptions = super.testOptions ++ Seq(TestArgument("junitxml"))
如何在sbt 0.10中说同样的话?
我是一个全新的Scala程序员,我以前没有Java经验; 我来自红宝石.我正在尝试运行我的第一个TDD示例程序.我计划一些非常小的东西,可能有5或6个测试.我正在尝试使用specs2 lib.我没有IDE,我通常用vim编程并从命令行执行一些东西.
如何从命令行使用我的.scala文件中的scala和specs2?
在快速入门中,他们提到了这一点:
scala -cp ... specs2.run HelloWorldSpec
Run Code Online (Sandbox Code Playgroud)
...)代表什么.我该怎么办?specs2.run.但在下载部分我只得到一个.jar文件.在哪里specs2.run?非常感谢.
这是我上一个问题的后续行动.
我想像下面的代码一样工作.我希望能够生成一个宏生成的方法:
case class Cat()
test[Cat].method(1)
Run Code Online (Sandbox Code Playgroud)
生成方法本身的实现使用宏("吸血鬼"方法):
// macro call
def test[T] = macro testImpl[T]
// macro implementation
def testImpl[T : c.WeakTypeTag](c: Context): c.Expr[Any] = {
import c.universe._
val className = newTypeName("Test")
// IS IT POSSIBLE TO CALL `otherMethod` HERE?
val bodyInstance = q"(p: Int) => otherMethod(p * 2)"
c.Expr { q"""
class $className {
protected val aValue = 1
@body($bodyInstance)
def method(p: Int): Int = macro methodImpl[Int]
def otherMethod(p: Int): Int = p
}
new …Run Code Online (Sandbox Code Playgroud) scala ×10
specs2 ×4
scala-macros ×3
macros ×2
scala-2.10 ×2
console ×1
sbt ×1
scalacheck ×1
scalatest ×1
scalaz ×1
set ×1
specs ×1