当您以某种形式将回调传递给另一个函数时,您通常必须完全填充某些接口以便能够传递此类回调.该回调接口通常会限制您可以抛出的异常类型.
对我来说最自然的方式是被调用的函数会自动重新抛出(或忽略)回调抛出的异常.即它会自动继承它可以从回调中抛出的异常列表.即它可以抛出的异常列表是通用的.
有可能已经可能吗?如果是这样,为什么它不被Javas库使用呢?
如果还不可能,为什么不呢?将其包含在语言中并不复杂.它会使一些事情更加清洁(见上文).
一个例子:
我只是偶然发现Comparator.compare不能抛出异常(参见此处的相关问题)和Collections.sort(或其他使用的函数Comparator)也没有.
如果Comparator.compare可以抛出的异常是通用的并且Collections.sort会抛出相同的东西,那对我来说会更有意义.这将以更加自然和干净的方式解决我的问题.
Compareable<Collection<T extends Compareable<T>>>Java中是否有任何实现(表现为C++ std::list<T>::operator<()或者std::set<T>::operator<())?
编辑:Comparator会更有意义......
我还在试验Java如何处理泛型.我偶然发现了一个事实/问题/事情,如果你有一个通用的界面A<T>,你不能真正检查一些对象是否实际实现A<B>或A<C>.
我想知道这是否会导致实际问题.
现在我尝试了这段代码:
static interface A<T> { void foo(T obj); }
static class B implements A<B> {
public void foo(B obj) { obj.bar(); }
void bar() {}
}
static {
assert (new B() instanceof A<?>);
((A<?>) new B()).foo(new Object());
}
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误(对于foo-call):
The method foo(capture#1-of ?) in the type Main.A<capture#1-of ?> is not applicable for the arguments (Object)
Run Code Online (Sandbox Code Playgroud)
我不知道这是为什么.Eclipse告诉我,foo演员之后的签名A<?>是foo(? obj)我认为的相同foo(Object obj).
的assert成功.
我试图弄清楚的是,当我调用 …
我有这些实现:
def vecAdd(v1, v2): return tuple(map(add, izip(v1,v2)))
def vecMul(v1, f): return tuple(map(mul, izip(v1,repeat(f))))
Run Code Online (Sandbox Code Playgroud)
那些没有用,因为add(和mul)被称为add((x,y)),即它只获得一个参数.
有什么功能基本上可以做到以下几点?
def funccaller_with_exposed_args(func):
return lambda args: func(*args)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,这可能是矫枉过正和过度设计,但总的来说,如果您能够在纯C代码中完成一个完整的重循环,这可能是非常重要的性能.
如何从Firefox(3或4)获取当前URL?到目前为止我找到的所有解决方案要么不起作用(例如那些"class curl"),要么是丑陋的黑客攻击对我来说没有解决办法(发送按键将URL复制到剪贴板).
有一些争论,在某些情况下,Fortran可能比C更快,例如,当涉及到别名时,我经常听说它比C更好地进行自动矢量化(这里有一些很好的讨论).
然而,对于简单的函数来计算Fibonaci数和Mandelbrot在一些复数时使用直接解决方案而没有任何技巧和额外的提示/关键字到编译器,我会期望他们真的执行相同.
C实施:
int fib(int n) {
return n < 2 ? n : fib(n-1) + fib(n-2);
}
int mandel(double complex z) {
int maxiter = 80;
double complex c = z;
for (int n=0; n<maxiter; ++n) {
if (cabs(z) > 2.0) {
return n;
}
z = z*z+c;
}
return maxiter;
}
Run Code Online (Sandbox Code Playgroud)
Fortran实现:
integer, parameter :: dp=kind(0.d0) ! double precision
integer recursive function fib(n) result(r)
integer, intent(in) :: n
if (n < 2) then
r = n …Run Code Online (Sandbox Code Playgroud) 我有多重继承(类A是基类,B派生自A,C派生自B)。 \nA有一个受保护的成员属性,我尝试在C.
考虑这段代码:
\n\n#include <iostream>\nusing namespace std;\n\nclass A {\nprotected:\n int x;\npublic:\n A() : x(42) {}\n};\n\nclass B : public A {\n};\n\nclass C : public B {\nprotected:\n typedef B Precursor;\n\npublic:\n void foo() {\n cout << Precursor::x << endl;\n cout << this->x << endl;\n }\n\n int get() {\n return Precursor::x;\n }\n\n int* getPtr() {\n // error: \xe2\x80\x98int A::x\xe2\x80\x99 is protected\n // error: within this context\n // error: …Run Code Online (Sandbox Code Playgroud) 我是Perl的新手并且正在尝试一下.我有这个代码:
use Digest::MD5 'md5';
use Data::Dumper::Perltidy;
my $data = "x";
my $digest = md5($data);
# print first 6 elements
print Dumper map(ord, split(//, $digest))[0..5];
Run Code Online (Sandbox Code Playgroud)
但是因语法错误而失败.我记得PHP有类似的问题,他们计划在未来的版本中解决这个问题.Perl是否还有这个问题,或者它只是错误的方法吗?怎么会是正确的方法?
这是代码:
@interface AppDelegate : NSObject <NSApplicationDelegate>
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
printf("My app delegate: finish launching\n");
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool
{
[NSApplication sharedApplication];
[NSApp setDelegate:[[AppDelegate alloc] init]];
[NSApp run];
}
}
Run Code Online (Sandbox Code Playgroud)
它崩溃[NSApp run]但我真的没有看到我错过了什么.如果我在[NSApp finishLaunching]之前添加了一个run,它会在那里崩溃.
如果我没有设置委托,它不会崩溃.
如果我之前引用了委托,它可以正常工作:
AppDelegate* appDelegate = [[AppDelegate alloc] init];
[NSApp setDelegate:appDelegate];
Run Code Online (Sandbox Code Playgroud)
所以我想它会因为ARC而在第一个版本中立即释放委托,因为委托可能只是一个弱引用,对吧?但是你应该怎么做相同的代码呢?
我只是偶然发现了非常糟糕的表现std::string.我预计std::string来自某些外部数据(例如std::string(X.c_str()))的新创建将大致等同于data = malloc(X.size())+ strcpy(data, X.c_str()),具有一些微小的恒定开销.
一些示例性能代码:
#include <string>
#include <string.h>
#include <assert.h>
static const char SampleString[] =
"Hello world. Hello world. Hello world. Hello world. Hello world. "
"Hello world. Hello world. Hello world. Hello world. Hello world. "
"Hello world. Hello world. Hello world. Hello world. Hello world. "
"Hello world. Hello world. Hello world. Hello world. Hello world. "
"Hello world. Hello world. Hello world. Hello world. Hello world. …Run Code Online (Sandbox Code Playgroud) 当我尝试用V8 运行一些JS代码(这个 + 这个)时我得到了这个错误(从两个弱点之前尝试过主,3.23.17,3.24.40,3.25.5; 3.23.0由于API不再起作用变化):
#
# Fatal error in ..\..\src\runtime.cc, line 785
# CHECK(V8::ArrayBufferAllocator() != NULL) failed
#
Run Code Online (Sandbox Code Playgroud)
许多其他JS代码已经有效,所以我想知道问题是什么.
它是在Win8上使用x64构建的.V8的构建正如官方文档中所述(使用gyp + MSVC 2012).我认为没有问题,因为它已经与大多数其他JS代码一起运行良好.
我认为V8本身可能存在问题,但不确定......
一些C++代码,但我不认为它有问题,因为它与其他JS代码一起工作正常:
#include <string>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <boost/noncopyable.hpp>
#include <v8.h>
// Create a new isolate (completely isolated JS VM).
struct V8Isolate : boost::noncopyable {
v8::Isolate* isolate;
V8Isolate() : isolate(v8::Isolate::New()) {}
~V8Isolate() { isolate->Dispose(); }
operator v8::Isolate*() { return isolate; }
v8::Isolate* operator->() { …Run Code Online (Sandbox Code Playgroud) <script type="text/javascript">
<!--//--><![CDATA[//><!--
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.5.0/load.js';
t.parentNode.insertBefore(s, t);
})();
//--><!]]>
</script>
Run Code Online (Sandbox Code Playgroud)
我对这个<!--//--><![CDATA[//><!--和我有点好奇//--><!]]>.我想这些技巧会欺骗那些不了解<script>-tag的老浏览器.但还<!--不够吗?究竟是什么CDATA东西?如果我不关心旧浏览器,那么这一切都已经过时了,对吧?(顺便说一下,如果我不把这些东西放在那里,有人会知道任何会混淆的浏览器吗?)
然后我想知道函数定义.为什么会这样?为什么不直接调用代码?是不是垃圾邮件全局命名空间?如果是这样,是不是更容易,更少hacky方式,例如只是把代码放入{}?
java ×3
c++ ×2
generics ×2
javascript ×2
appkit ×1
applescript ×1
arrays ×1
c ×1
cocoa ×1
comparable ×1
exception ×1
firefox ×1
fortran ×1
html ×1
inheritance ×1
interface ×1
julia ×1
macos ×1
objective-c ×1
perl ×1
python ×1
slice ×1
stl ×1
string ×1
v8 ×1