为什么Kotlin需要函数参考语法?

Kotlin docs表示支持高阶函数 。 为什么在将顶级函数作为参数传递时,语言甚至需要一个::function语法? 鉴于: fun isOdd(x: Int) = x % 2 != 0 val numbers = listOf(1, 2, 3) println(numbers.filter(::isOdd)) // here. 为什么不只是 fun isOdd(x: Int) = x % 2 != 0 val numbers = listOf(1, 2, 3) println(numbers.filter(isOdd)) // simple and makes more sense 更多关于这里的函数参考语法 。

在Kotlin中创建一个抽象类的实例

我是Kotlin的新手,我试图在Android项目中使用它。 我有这个代码: public var oneTouchTimer: CountDownTimer = CountDownTimer(500, 100) { override fun onTick(l: Long) { } override fun onFinish() { } } 这是抛出错误: Cannot create an instance of an abstract class. 基本上我试图创建一个CountDownTimer的实例,并不知道如何将其转换为Kotlin。 这是Java中的代码: CountDownTimer oneTouchTimer = new CountDownTimer(500, 100) { @Override public void onTick(long l) { } @Override public void onFinish() { } };

kotlin中的嵌套对数组

我想在kotlin中使用嵌套对,比如"a" to {"b" to "c"} 我努力了 : "teachers" to {"a" to "c"; "c" to "d"} 但是当我调试这个,数据类型是: (teachers, () -> kotlin.Pair<kotlin.String, kotlin.String>) 如何使用这个? 如果不使用 "a" to mapOf("a" to "b"…) 可能吗?

Kotlin的指称和结构平等

Kotlin的 指称平等和结构平等有什么区别? val a = File("/myfile.txt") val b = File("/myfile.txt") val sameRef = a === b 和: val a = File("/myfile.txt") val b = File("/myfile.txt") val both= a == b

Kotlin中的Room Persistence lib实现

我在kotlin中为我的数据库实现实现了Room persistence lib。 以下是我的Entity , Dao和Database类: Food.kt @Entity class Food(@ColumnInfo(name = "food_name") var foodName: String, @ColumnInfo(name = "food_desc") var foodDesc: String, @ColumnInfo(name = "protein") var protein: Double, @ColumnInfo(name = "carbs") var carbs: Double, @ColumnInfo(name = "fat") var fat: Double) { @ColumnInfo(name = "id") @PrimaryKey(autoGenerate = true) var id: Long = 0 @ColumnInfo(name = "calories") var […]

Kotlin – 如何获得注释属性值

说,我有一个Kotlin类与注释: @Entity @Table(name="user") data class User (val id:Long, val name:String) 我怎样才能从@Table注释获得名称属性的值? fun <T> tableName(c: KClass<T>):String { // i can get the @Table annotation like this: val t = c.annotations.find { it.annotationClass == Table::class } // but how can i get the value of "name" attribute from t? }

Kotlin webview崩溃

我想从一个Fragment访问一个WebView和kotlin不停地说: kotlin.TypeCastException: null cannot be cast to non-null type android.webkit.WebView 我试图使其为默认,然后初始化它,但也不起作用。 任何建议,我怎样才能使其工作? 我的代码: private lateinit var webView: WebView override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater!!.inflate(R.layout.editor_edit_fragment, container, false) webView = view.findViewById(R.id.webview) as WebView resumeWebView() return view } XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webview" […]

用Mvvmfx框架使用kotlin

我正在用JavaFx开发一个桌面应用程序。 我有兴趣使用Mvvmfx作为框架,并想知道是否可以使用Kvlin和Mvvmfx来避免所有样板代码? 有没有这个地方的例子?

kotlin – 领域和包裹

我用android studio开发了android项目。 我想在kotlin中使用onSaveInstanceState()来保存领域对象。 我的代码是 @Parcel( implementations = arrayOf(UserRealmProxy::class), value = Parcel.Serialization.BEAN, analyze = arrayOf(User::class)) open class User : RealmObject() { open var name: String? = null @ParcelPropertyConverter(ListParcelConverter::class) open var Items: RealmList<Item>? = null } 但编译时出现一些错误: 'Unresolved reference: UserRealmProxy' 'An annotation parameter must be a compile-time constant' 当然,UserRealmProxy已经存在了,因为项目已经被编译了。 也@ParcelPropertyConverter(ListParcelConverter::class)不起作用。 它在运行时会导致异常: 'java.io.NotSerializableException: io.realm.RealmList' 但是这个代码在java中编译得很好。 我需要你的帮助。

验证数据类参数Kotlin

如果我使用Kotlin数据类为我的值对象建模,处理验证的最佳方法是什么? 看起来像init块是唯一的逻辑地方,因为它在主构造函数之后执行。 data class EmailAddress(val address: String) { init { if (address.isEmpty() || !address.matches(Regex("^[a-zA-Z0-9]+@[a-zA-Z0-9]+(.[a-zA-Z]{2,})$"))) { throw IllegalArgumentException("${address} is not a valid email address") } } } 使用JSR-303示例 缺点是需要加载时间编织 @Configurable data class EmailAddress(@Email val address: String) { @Autowired lateinit var validator: Validator init { validator.validate(this) } }