小编Alk*_*dis的帖子

CoroutineScope.async 上的异常传播如何工作?

我看到多个消息来源声称 async{} 块内发生的异常不会传递到任何地方,仅存储在实例中Deferred。据称,该异常仍然是“隐藏的”,并且仅在调用时影响外部事物await()launch{}这通常被描述为和之间的主要区别之一async{}这是一个例子

异步代码中未捕获的异常存储在生成的 Deferred 中,并且不会传递到其他任何地方,除非进行处理,否则它将被静默丢弃

根据这一说法,至少按照我的理解,以下代码不应该抛出异常,因为没有人调用await:

// throws
runBlocking {
  async { throw Exception("Oops") }
}
Run Code Online (Sandbox Code Playgroud)

然而,异常还是被抛出了。这里也讨论了这一点,但通过阅读本文我无法真正理解为什么。

所以在我看来,当异步抛出时,即使await()没有被调用,也会在父作用域上传播“取消信号”。也就是说,异常并没有真正隐藏起来,也没有默默地被丢弃,正如上面引用的那样。我的假设正确吗?

现在,如果我们传递 a SupervisorJob(),代码不会抛出

// does not throw
runBlocking {
  async(SupervisorJob()) { throw Exception("Oops") }
}
Run Code Online (Sandbox Code Playgroud)

这似乎是合理的,因为主管的工作就是要承受失败。

现在是我完全不明白的部分。如果我们传递Job(),代码仍然会运行而不会抛出异常,即使Job()应该将失败传播到其父作用域:

// does not throw. Why?
runBlocking {
  async(Job()) { throw Exception("Oops") }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,为什么不传递 Job 会抛出异常,但传递 Job 或 SupervisorJob 不会抛出异常?

async-await kotlin kotlin-coroutines

8
推荐指数
1
解决办法
722
查看次数

Kotlin,如何在扩展和实现时克服意外覆盖

我尝试在 kotlin 中创建一个自定义异常,并且还实现了 GraphQLError 接口,该接口需要 getMessage() 方法。

如果我尝试实现该方法,我的 IDE 会警告我:

Accidental override: The following declarations have the same JVM signature (getMessage()Ljava/lang/String;): 
public open fun <get-message>(): String? defined in eu.mojo.presentation2018.error.ShopException
public open fun getMessage(): String defined in eu.mojo.presentation2018.error.ShopException
Run Code Online (Sandbox Code Playgroud)

但如果我删除它:

Class 'ShopException' is not abstract and does not implement abstract member
public abstract fun getMessage(): String! defined in graphql.GraphQLError
Run Code Online (Sandbox Code Playgroud)

我在网上搜索了解决方案,但都需要对导致冲突的字段“消息”进行一些控制。在我的情况下,此控件不存在,因为字段消息是在我尝试扩展的 Exception 类中定义的。

这是我的类定义:

class ShopException(code:ErrorCode) : Exception(), GraphQLError {...}
Run Code Online (Sandbox Code Playgroud)

inheritance kotlin

7
推荐指数
1
解决办法
3183
查看次数

Java流:对链接使用可选的filter()操作

注意:这个问题是相关的java.util.Optional.

在处理流时,我经常使用这样的逻辑:

 Stream<FooBar> stream = myInitialStream();
 if (needsFilter1) stream = stream.filter(c -> whatever1());
 if (needsFilter2) stream = stream.filter(c -> whatever2());
 ...
 return stream.collect(toList());
Run Code Online (Sandbox Code Playgroud)

我想要实现的是使用链接将上面的代码转换为单个表达式.我发现这更具可读性和直接性.到目前为止,我发现实现这一目标的唯一方法是:

return myInitialStream()
       .filter(needsFilter1? c->whatever1() : c->true)
       .filter(needsFilter2? c->whatever2() : c->true)
       .collect(toList());
Run Code Online (Sandbox Code Playgroud)

尽管如此,这会对那些琐碎的c->truelamdas 进行不必要的调用,这可能会在扩展时产生一些性能成本.

所以我的问题是:是否有更好的方法来生成包含可选过滤的链式流表达式?

更新:也许我没有说清楚,但我的问题是找到单表达式解决方案.如果非要使用多个语句(初始化谓词,例如),我还可以用我的问题基本上不相同的第一个代码块.

java lambda predicate java-8 java-stream

6
推荐指数
1
解决办法
220
查看次数

Angular 7:存在查询参数时出现不需要的重定向

我使用 @Angular/router 和 Angular 7。

我的目标是在我的页面之一上使用任意数量的(可选)查询参数,特别是在 /pages/components 中

我面临的问题是,每当我在网址栏中输入一些查询参数时,我就会被重定向到一个奇怪的位置。如果存在任何查询参数,此重定向会发生在我的所有页面上。

例子:

我正在努力理解这个重定向。

看起来第一个查询参数的前 3 个字母被截断,查询字符串的其余部分被转义,并且由于某种原因,我总是以 /pages/components/something 结束,即使我输入的 url 完全是一个不同的页面(也许是因为组件页面是我唯一在 RouterModule 上带有参数的页面?)。

这是我的路由模块:

const appRoutes: Routes = [
  {path: '', component: LoginPageComponent, runGuardsAndResolvers:'always', pathMatch: 'full'},
  {path: 'pages/components', component: ComponentsPageComponent, runGuardsAndResolvers: 'always'},
  {path: 'pages/classes', component: ClassesPageComponent, runGuardsAndResolvers: 'always'},
  {path: 'pages/components/:id', component: ComponentsPageComponent, runGuardsAndResolvers: 'always'},
  {path: 'pages/dashboard', component: DashboardPageComponent, runGuardsAndResolvers: 'always'},
  {path: 'pages/users', component: UserAdminisitrationComponent, runGuardsAndResolvers: 'always', canActivate: [UserRoleGuardService]},
  {path: 'pages/reports', component: ReportsPageComponent, runGuardsAndResolvers: 'always'},
  {path: 'pages/jobs', …
Run Code Online (Sandbox Code Playgroud)

router query-string angular7

5
推荐指数
1
解决办法
2325
查看次数

html:从 onclick 属性访问鼠标事件

我知道如果我用 js 分配鼠标侦听器,我可以访问鼠标事件:

myElement.addEventListener("click", e => console.log(e.pageX))
Run Code Online (Sandbox Code Playgroud)

我的问题是:通过 html 属性分配侦听器时是否可以访问鼠标事件?

<div onclick="alert('where is the mouse event?')"></div>
Run Code Online (Sandbox Code Playgroud)

html javascript events

4
推荐指数
1
解决办法
826
查看次数

Yocto sumo,使用 vmdk 进行元虚拟化

我尝试使用 yocto sumo 构建一个自定义的 linux 发行版,其中将包括 docker。我下载了 yocto 核心,并添加了元虚拟化层(来自相扑分支)。我能够成功构建和运行它。问题是我也想创建一个 vmdk 映像。当我在 local.conf 中添加相应的行以包含此图像时,出现以下错误:

/home/user/yocto/sumo/meta-virtualization/recipes-extended/images/xen-guest-image-minimal.bb: No IMAGE_CMD defined for IMAGE_FSTYPES entry 'vmdk' - possibly invalid type name or missing support class
ERROR: /home/user/yocto/sumo/meta-virtualization/recipes-extended/images/kvm-image-minimal.bb: No IMAGE_CMD defined for IMAGE_FSTYPES entry 'vmdk' - possibly invalid type name or missing support class
ERROR: /home/user/yocto/sumo/meta-virtualization/recipes-extended/images/xen-image-minimal.bb: No IMAGE_CMD defined for IMAGE_FSTYPES entry 'vmdk' - possibly invalid type name or missing support class
ERROR: /home/user/yocto/sumo/meta-virtualization/recipes-extended/images/cloud-image-guest.bb: No IMAGE_CMD defined for IMAGE_FSTYPES entry 'vmdk' - possibly invalid type name …
Run Code Online (Sandbox Code Playgroud)

docker yocto vmdk

3
推荐指数
1
解决办法
1436
查看次数

llvm,通过 C++ API 定义字符串和数组

我开发了一个玩具编译器,并尝试实现字符串和数组。我注意到 clang 总是为这些类型创建一个全局变量,即使它们是在函数中定义的。我想这是有充分理由的,所以我尝试这样做。

我的问题是我不知道如何通过 C++ API 来做到这一点。 kalidscope 教程不涵盖字符串和数组,因此我找到的唯一来源是文档

在 Module 类的文档中,有函数getOrInsertGlobal,它看起来相关,但我无法理解如何设置全局的实际值。函数参数仅包括变量的名称和类型。那么价值去哪儿了呢?

所以问题是:如何定义一个全局字符串,例如“hello”或数组,例如llvm c++ API中的[i32 1, i32 2]?任何例子将非常感激。

c++ llvm

3
推荐指数
1
解决办法
2560
查看次数

Flex/Bison:不能使用语义类型

我尝试创建一个 c++ flex/bison 解析器。我使用本教程作为起点,并没有更改任何野牛/flex 配置。我现在坚持尝试对词法分析器进行单元测试。

我的单元测试中有一个直接调用 yylex 的函数,并检查它的结果:

private: static void checkIntToken(MyScanner &scanner, Compiler *comp, unsigned long expected, unsigned char size, char isUnsigned, unsigned int line, const std::string &label) {
    yy::MyParser::location_type loc;
    yy::MyParser::semantic_type semantic; // <---- is seems like the destructor of this variable causes the crash

    int type = scanner.yylex(&semantic, &loc, comp);
    Assert::equals(yy::MyParser::token::INT, type, label + "__1");

    MyIntToken* token = semantic.as<MyIntToken*>();
    Assert::equals(expected, token->value, label + "__2");
    Assert::equals(size, token->size, label + "__3");
    Assert::equals(isUnsigned, token->isUnsigned, label + "__4");
    Assert::equals(line, …
Run Code Online (Sandbox Code Playgroud)

c++ unit-testing bison flex-lexer

1
推荐指数
1
解决办法
670
查看次数