import Data.Monoid
times :: Monoid a => Int -> a -> a
times i = mconcat . replicate i
main =
print $ times 5 5
Run Code Online (Sandbox Code Playgroud)
此代码提供以下错误:
Ambiguous type variable `a0' in the constraints:
(Monoid a0) arising from a use of `times'
at :7:11-15
(Show a0) arising from a use of `print'
at :7:3-7
(Num a0) arising from the literal `5'
at :7:19
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', …Run Code Online (Sandbox Code Playgroud) 我想将某个文件复制到指定的路径.此指定的路径具有许多目录的层次结构,这些目录在事先不存在并且需要在复制期间创建.
我尝试过shutil.copy*函数,但它们似乎都要求预先创建目标路径上的目录.
是否有任何功能可以根据需要设置这些目录并复制文件?
用法示例:
copy_file('resources/foo.bar', expanduser('~/a/long/long/path/foo.bar'))
Run Code Online (Sandbox Code Playgroud) 假设我有一个名为的方法foo,对于某些输入值集,预计会成功完成并返回结果,而对于其他一些值集,预计会抛出某个异常。此方法需要先设置一些东西,然后才能进行测试。
鉴于这些条件,将成功和失败测试集中在一个测试中更好,还是应该在单独的测试方法中维护这些案例?
换句话说,以下两种方法中哪一种更可取?
方法一:
@Test
public void testFoo() {
setUpThings();
// testing success case
assertEquals(foo(s), y);
// testing failure case
try {
foo(f);
fail("Expected an exception.")
} catch (FooException ex) {
}
}
Run Code Online (Sandbox Code Playgroud)
方法二:
@Test
public void testFooSuccess() {
setUpThings();
assertEquals(foo(s), y);
}
@Test
public void testFooFailure() {
setUpThings();
try {
foo(f);
fail("Expected an exception.")
} catch (FooException ex) {
}
}
Run Code Online (Sandbox Code Playgroud) 我通常遵循的两种方法是:
将HTML转换为字符串,然后针对目标字符串对其进行测试.这种方法的问题在于它太脆弱了,并且会出现非常频繁的假阴性,比如说某些地方有额外的空白.
将HTML转换为字符串并将其作为XML解析,然后使用XPath查询在特定节点上进行断言.这种方法运行良好,但并非所有HTML都带有结束标记并解析它,因为在这种情况下XML失败.
这两种方法都有严重的缺陷.我想这种测试必须有一套完善的方法(或方法).它是什么?
有没有办法从规范2运行特定的测试Specification?例如,如果我有以下内容:
class FooSpec extends Specification {
"foo" should {
"bar" in {
// ...
}
"baz" in {
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想有办法只运行FooSpec > "foo" > "bar"(在这里使用一些任意表示法).
以下两个语句在语义上是否相同?
#1 person p("Rahul", 20);
#2 person const &p = person("Rahul", 20);
编辑:
对不起,我想问一下以下两个语义是否相同:
#1 person const p("Rahul", 20);
#2 person const &p = person("Rahul", 20);
这编译:
class Ex1 {
public int show() {
try {
int a=10/10;
return 10;
}
catch(ArithmeticException e) {
System.out.println(e);
}
finally {
System.out.println("Finally");
}
System.out.println("hello");
return 20;
}
}
Run Code Online (Sandbox Code Playgroud)
另一方面,这不是:
class Ex15 {
public int show() {
try {
int a=10/0;
return 10;
}
catch(ArithmeticException e) {
System.out.println(e);
}
finally {
System.out.println("Finally");
return 40;
}
System.out.println("hello");
return 20;
}
}
Run Code Online (Sandbox Code Playgroud)
并给出无法访问的语句System.out.println("hello"); 错误.为什么会这样?
为什么以下编译?
scala> val ch1 = 'a' + 'b'
ch1: Int = 195
Run Code Online (Sandbox Code Playgroud)
但以下不是?
scala> var ch1 = 'a'
ch1: Char = a
scala> ch1 += 'b'
<console>:9: error: type mismatch;
found : Int
required: Char
ch1 += 'b'
^
scala> ch1 = ch1 + 'b'
<console>:8: error: type mismatch;
found : Int
required: Char
ch1 = ch1 + 'b'
^
Run Code Online (Sandbox Code Playgroud)
为什么错误信息如此误导?为什么它说required: Char我什么时候通过显然是一个Char?
经过很长一段时间,我正在尝试使用Scala Eclipse IDE.我刚刚安装了Eclipse和所说的Scala插件,但是我无法正常工作.在所有Scala文件中,我得到如下错误(请参阅工具提示):

这个项目使用Gradle进行构建,我还安装了最新版本的Groovy插件.请帮我把这件事搞定.谢谢.
编辑:
谢谢大家.问题解决了.看来我的Scala插件安装有问题.完全重新安装所有东西(包括Eclipse)都有帮助.此外,这次我没有安装Groovy插件.
我有一个类,其构造函数如下所示:
abstract class BasePanel extends JPanel {
public BasePanel(A a) {
// initializing fields from values passed to ctor
this.a = a;
// initializing gui components
initializeComponents();
setupPanels();
concludeUiSetup();
}
// stuff
}
Run Code Online (Sandbox Code Playgroud)
在构造函数中,首先初始化要使用传递给构造函数的值进行初始化的字段.然后按顺序调用UI设置所需的其他方法.需要在子类中重写其中两个方法,以便针对它们进行UI设置.
现在考虑一个FooPanel扩展的类BasePanel.它在构造函数中需要更多的初始化参数.
class FooPanel extends BasePanel {
public FooPanel(A a, B b) {
super(a);
this.b = b;
}
@Override
public void initializeComponents() {
super.initializeComponents();
// I require b here, but oops, b is not initialized at this point, and so
// this will …Run Code Online (Sandbox Code Playgroud) java ×4
scala ×3
unit-testing ×3
testing ×2
c++ ×1
eclipse ×1
gradle ×1
groovy ×1
haskell ×1
html ×1
junit ×1
oop ×1
python ×1
python-3.x ×1
refactoring ×1
scala-ide ×1
semantics ×1
specs2 ×1
swing ×1
web-testing ×1