我试图使用TooTallNate的Java-Websocket库来创建一个websocket客户端,它接收来自coinbase交换websocket流的消息.我正在将我用Python制作的程序移植到Java中,因为Python中存在并行化瓶颈,据我所知,我在Java中做的事情与在Python中做的相同.这是我使用这个websocket lib在Python中打开连接的代码(这可以按预期工作):
ws = websocket.create_connection("wss://ws-feed.exchange.coinbase.com", 20)
ws.send(json.dumps({
"type": "subscribe",
"product_id": "BTC-USD"
}))
Run Code Online (Sandbox Code Playgroud)
这是我的整个Java类:
public class CoinbaseWebsocketClient extends WebSocketClient {
private final Gson gson = new Gson();
private CoinbaseWebsocketClient(URI serverURI) {
super(serverURI, new Draft_17());
connect();
}
private static URI uri;
private static CoinbaseWebsocketClient coinbaseWebsocketClient;
static {
try {
uri = new URI("wss://ws-feed.exchange.coinbase.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
protected static CoinbaseWebsocketClient get() {
if (coinbaseWebsocketClient == null) {
coinbaseWebsocketClient = new …Run Code Online (Sandbox Code Playgroud) 我主要了解检查异常的潜在问题以及为什么Kotlin省略它们.但是,我遇到的问题是我找不到任何简单的方法来向调用者明确指出函数可能抛出的异常.
我已经在Python中遇到过无数次问题,我的程序在运行了几个月之后会崩溃,因为我没有意识到我正在使用的某个库中的函数会引发一个特殊的异常.虽然被迫捕获异常可能会有很大问题,但很清楚地看到函数可以抛出的所有潜在异常.
回到这个问题,是否有任何简单的方法可以查看函数在Kotlin中抛出的异常?对于用Kotlin调用的Java编写的方法怎么样?即使只是在工具(intelliJ).我不打算用javadoc或kdoc编写它,因为你正在使用的函数的编写者可能已经省略了它.
exception-handling exception intellij-idea kotlin unchecked-exception
我已经使用了Jupyter笔记本一段时间没有任何问题.但是,我几乎使用IntelliJ进行编程,并希望尝试将ipython工作流程移动到IntelliJ中.我按照这里提供的说明操作,一切似乎工作正常,除非我去运行一个单元格,它一直试图启动内核,即使内核应该已经运行.
当我按照链接时,我可以像往常一样在Jupyter中使用内核,但是我不能在IntelliJ中使用它.
当我从Intellidle IDEA中的Gradle Groovy DSL迁移到Kotlin DSL后尝试构建项目时,我收到了一个非常无法解释的错误.
Unsupported method: IdeaModuleDependency.getDependencyModule().
The version of Gradle you connect to does not support that method.
To resolve the problem you can change/upgrade the target version of Gradle you connect to.
Alternatively, you can ignore this exception and read other information from the model.
我正在使用gradle 5.0包装器,因此据我所知,更新不是一个选项.
我也使用最新版本的IntelliJ,所有插件都是最新的.
IntelliJ IDEA 2018.3(终极版)Build#IU-183.4284.148,建于2018年11月21日
我用Gradle构建的项目是一个多模块Kotlin-JVM项目.
我当然排除了任何涉及违反数据类规则的原因。因此,如果你知道你不需要继承它,例如(尽管我的理解是规则在 Kotlin 1.1 中消失了)。
我正在使用JSR363的参考实现.我尝试了很多这方面的变化,但我会以此代码为例.
ServiceProvider provider = ServiceProvider.current();
QuantityFactory<Length> lengthFactory = provider.getQuantityFactory(Length.class);
Quantity<Length> first = lengthFactory.create(5, Units.METRE.divide(100.0));
Quantity<Length> second = lengthFactory.create(3, Units.METRE);
System.out.println(second.add(first));
Run Code Online (Sandbox Code Playgroud)
这打印503.0米.显然有些东西是非常错误的,这应该是3.05米.我发现很难相信这实际上是图书馆的一个错误,我希望有人可以告诉我我错过了什么.
我得到了这个令人困惑的错误,似乎是在Kotlin编译器中,我不介意更改我的代码,但我想弄清楚我是否确实做错了什么.我认为它遇到问题的代码是这样的:
inline fun <T> fiberListener(bufferSize: Int = 50,
policy: Channels.OverflowPolicy = Channels.OverflowPolicy.BACKOFF,
singleProducer: Boolean = true,
singleConsumer: Boolean = true,
crossinline action: (T) -> Unit): SendPort<T> {
val channel = Channels.newChannel<T>(bufferSize, policy, singleProducer, singleConsumer)
fiber { channel.forEach(action) }
return channel
}
Run Code Online (Sandbox Code Playgroud)
不知道还有什么要说的,我可以说这不是内联的,但我认为这段代码应该可行.我正在使用Kotlin 1.0.4和Gradle.另一方面,我并不认为这实际上提供了性能提升,我对交叉线仍然有点不清楚.但无论哪种方式似乎它应该工作,我得到没有IDE或编译器错误,除了这个内部的.
Error:(37, 45) Kotlin: [Internal Error] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't inline method call 'fiberListener' into
local final fun <anonymous>(module: org.tenkiv.nexus.thermal.module.ProducerModule): kotlin.Unit defined in org.tenkiv.nexus.thermal.control_point.StateMaintainer.<init>[AnonymousFunctionDescriptor@392d1483]
{ module ->
module.levelSensor?.addListener(fiberListener { onLevelEvent() })
}
Cause: fiberListener$default (ILco/paralleluniverse/strands/channels/Channels$OverflowPolicy;ZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lco/paralleluniverse/strands/channels/SendPort;:
L0 …Run Code Online (Sandbox Code Playgroud) 我在Android中制作了一个布局(XML文件),它有两个子视图(一个位于屏幕右侧,另一个位于左侧).我希望右边的视图占据一个预定的空间(在dp中)并让左边的视图占用所有剩余的空间达到极限,此时它将停止扩展,这两个布局将只是随着屏幕变大,进一步分开.
奇怪的是,如果我希望右侧的视图是扩展的视图,并且左侧的视图是占用预设空间的视图,这将非常容易.如果您将每个视图设置为所需的宽度(在水平线性布局中),如果两个视图都不适合,Android将自动缩小左侧的视图.
我想在一个布局文件中执行此操作; 这种布局已经设计用于sw512dp-land和sw765dp-land之间的显示.
如果我能找到一种方法让Android缩小左侧的布局(当布局都不能适合指定的大小时),下面的代码就可以工作了.但默认情况下,系统会先缩小右侧的布局.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/left"
android:layout_width="150dip"
android:layout_height="fill_parent"
android:background="@color/red" >
</RelativeLayout>
<LinearLayout
android:id="@+id/right"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/green" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果我不需要左边的布局来停止某个点的扩展,那么这个代码(来自@Lokesh)就可以工作了.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="@+id/right"
android:background="@color/red" >
</LinearLayout>
<LinearLayout
android:id="@+id/right"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/green" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如果有人认为这是不可能的,那么我也可以采取实际操作或改变我的布局方法.
谢谢!
android android-layout android-xml android-screen android-screen-support
在Kotlin中,数据类可以像这样进行解构:
fun main(args: Array<String>) {
val thing = Stuff(1, "Hi", true)
val(thing1, thing2, thing3) = thing
println(thing1)
}
data class Stuff(val thing1: Int, val thing2: String, val thing3: Boolean)
Run Code Online (Sandbox Code Playgroud)
我可能会误读文档,或者我可能找不到一个例子,但我正在寻找一种方法来实现非数据类的自定义解构.这在Kotlin有可能吗?
我试图检查一个类型是否符合if表达式的另一个类型,如下所示:
if (String::class is Any::class)
Run Code Online (Sandbox Code Playgroud)
这给了我不支持空左侧的错误类文字.任何人都可以详细说明这个错误和/或告诉我应该怎么做这个检查?
编辑(澄清):我不能进行相等检查,因为我需要知道左边的类是与右边的类匹配还是它的子类.因此,如果左侧类的实例可以安全地转换为右侧的类.
基本上我需要相当于:
if ("A string" is Any)
Run Code Online (Sandbox Code Playgroud)
但是没有String实例,String只是在这里使用了一个例子.
我无法找到关于何时实例化Kotling单身人士的任何信息.我假设这是他们第一次被访问,但我无法在任何地方确认.
object Singleton{
val thing1 = 2
val thing2 = "Hello"
}
Run Code Online (Sandbox Code Playgroud)
该对象何时被实例化?首次访问房产时?当访问包装中的某些东西时?程序第一次运行时?