Java函数转换为Kotlin失败

尝试将一些java代码转换为kotlin,给定以下方法 public class Option<T> { public <U> Option<U> map(Function<T, U> mapper) { throw new IllegalStateException(); } } 科特林转换将给这个 我不明白这里有什么问题,我如何在kotlin中创建等价的方法? (这是java.util.Function ) PS不能拿出一些更好的问题总结…随时改变。

Swift如果让和其他在Kotlin

在Kotlin中有一个相当于下面的Swift代码? if let a = b.val { } else { }

在Kotlin中渲染json响应

我是Kotlin和Java的新手。 我试图做一个API调用,然后在Kotlin中呈现它。 这是我得到的JSON响应: [ { "AWBNo": "1326217373504", "AuthKey": "Valid", "OrderNo": "SGHRGR15073TCC", "ReturnMessage": "Successful", "ShipmentSummary": [ { "PickUpDate": "08-10-2017", "PickUpTime": "0015", "OriginLocation": "DEL/WDL, Delhi NCR, DELHI", "DestinationLocation": "", "Weight": "0", "ExpectedDeliveryDate": "10/11/2017 12:21:32 AM", "Status": "Delivered", "StatusCode": "DLVD", "StatusDate": "11-10-2017", "StatusTime": "1316", "Location": "Berhampur, Berhampur, ORISSA", "Comment": "Shipment Delivered by SR: RAKESH, DeliveryDate:2017-10-11 1316, Receiver Name: […]

Kotlin – 抛出自定义异常

我怎样才能在Kotlin中抛出一个自定义异常? 我真的没有那么多的文件… 在文档中,描述了每个异常需要的内容,但是我如何实现呢?

更新到kotlin 1.0.0-beta后,Kotlin cli应用程序无法正常工作

我将问题简化为以下简单的设置 – 只是一个问候世界。 这个工作: https://github.com/ligi/MinimalKotlinCommandLine 当我切换到kotlin:1.0.0-beta-4584并运行(gradle clean run)时出现以下错误: Error: Could not find or load main class minimalkotlincommandline.MinimalkotlincommandlinePackage 用这个build.gradle: buildscript { repositories { mavenCentral() } dependencies { classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.0-beta-4584' } } apply plugin: "kotlin" apply plugin: 'application' mainClassName = "minimalkotlincommandline.MinimalkotlincommandlinePackage" repositories { mavenCentral() } dependencies { compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.0-beta-4584' } 和这个Main.kt:包minimalKotlincommandline fun main(args: Array<String>) { println("Hello world […]

Kotlin编译为不可运行的JavaScript

我在kotlin(即sample-kotlin-project )中有一个项目,使用下面的gradle脚本使用gradle编译为javascript: buildscript { ext.kotlin_version = '1.1.4-3' repositories { mavenCentral() maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.moowork.gradle:gradle-node-plugin:1.2.0' } } apply plugin: 'kotlin2js' apply plugin: 'com.moowork.node' repositories { mavenCentral() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version" testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version" } task wrapper(type: Wrapper) { gradleVersion = "4.1" } [compileKotlin2Js, compileTestKotlin2Js]*.configure { kotlinOptions.moduleKind = "commonjs" […]

Kotlin:可空的财产代表可观察

在Kotlin中,我们可以定义一个非null属性的observable, var name: String by Delegates.observable("<no name>") { prop, old, new -> println("$old -> $new") } 然而这是不可能的 var name: String? by Delegates.observable("<no name>") { prop, old, new -> println("$old -> $new") } 定义可空属性的可观察值的方法是什么? 编辑:这是编译错误 Property delegate must have a 'setValue(DataEntryRepositoryImpl, KProperty<*>, String?)' method. None of the following functions is suitable: public abstract operator fun setValue(thisRef: […]

Android的Kotlin扩展开销

在代码中使用kotlin扩展是个好主意吗? 我非常想念iOS的扩展,但这是在android中使用这些东西的好方法吗? 请参阅http://antonioleiva.com/kotlin-android-extension-functions/ 有没有更好的解决方案呢?

Kotlin当()局部变量的引入

假设我有一个名为doHardThings()函数可能会返回各种不同的类型,我想根据返回的类型采取行动。 在Scala中,这是match构造的常见用法: def hardThings() = doHardThings() match { case a: OneResult => // Do stuff with a case b: OtherResult => // Do stuff with b } 我正在努力弄清楚如何在Kotlin中干净地完成这个工作,而不用为doHardThings()引入一个临时变量: fun hardThings() = when(doHardThings()) { is OneResult -> // Do stuff… with what? is OtherResult -> // Etc… } 什么是这种常见用例的惯用Kotlin模式?

用Kotlin 1.1.2-4编译时的循环依赖关系,而不是1.1.2-3

我得到了下面的错误 * What went wrong: Circular dependency between the following tasks: :app:compileProductionDebugKotlin \— :app:kaptProductionDebugKotlin \— :app:compileProductionDebugKotlin (*) (*) – details omitted (listed previously) 当我使用Kotlin版本1.1.2-4(好的时候我用1.1.2-3)。 在我的应用程序build.gradle我有以下 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' 当我删除kotlin-kapt ,它编译得很好。 我做错了什么,或者这是一个Kotlin错误?