Tag: lambda

在Kotlin中可变参数的lambdatypes?

是否有可能作为内联lambda参数接收一个函数types,它具有保证的返回typesR但可变参数的数量 ? 为简单起见,请考虑以下几点: inline fun Boolean?.tfn(tru:()->R, fls:()->R, nul:()->R) = if (this == null) nul() else if (this) tru() else fls() 让我们假设我有其他的内联函数,也可能是(A)->R或(A,B)->R或(A,B,C)->Rtypes的lambda参数 ,其参数我想传递给这个函数。 他们都将提供一个R但是他们都必须被调用来获得R ,而不知道这个函数中参数的数量/types/值。 有没有办法一般修改上面的function: 捕获返回R作为其输入的任何lambda参数的一般情况 避免对将调用此函数的其他内联函数进行任何更改 保持内联效率

如何从调用者Kotlin指定的lambda中解除DialogFragment

我从下面的片段运行GirlQualitiesChoiceDialog: girlType.setOnClickListener { GirlQualitiesChoiceDialog.show(activity, InitialDataSet.instance.QUALITY_TYPE_COLOR){ Picasso.with(context).load(it.resId).into(colorTypeImage as ImageView) //GirlQualitiesChoiceDialog.this.dismiss() – need to dismiss the created dialog } } 在哪里指定侦听器。 我需要的是在我加载图片后关闭对话框。 GirlQualitiesChoiceDialog.this.dismiss()不起作用。 请问有谁能告诉我,我怎么能解雇GirlQualitiesChoiceDialog在这里? 假设我需要将对话回调以某种方式传递给lambda,但无法实现。 下面是我的对话框的代码: class GirlQualitiesChoiceDialog : DialogFragment() { companion object { val ARG_QUALITY_TYPE = “ARG_QUALITY_TYPE” private lateinit var itemClickInterface: (Quality) -> Unit fun show(activity: Activity, qualityType: Int, itemClick: (Quality) -> Unit) { itemClickInterface = […]

kotlin:用多个lambdaexpression式调用高阶函数

有可能调用具有多个lambda函数的函数吗? 如果是这样,我怎样才能调用以下function? fun post(path: String, params: JSONObject, completionHandler: (response: JSONObject?) -> Unit, errorCompletionHandler: (error: VolleyError?) -> Unit )

使用kotlin lambda回调的unit testing场景

假设我们有以下function来测试 fun loadData(dataId: Long, completion: (JsonElement?, Exception?) -> Unit { underlayingApi.post(url = “some/rest/url”, completion = { rawResult, exception -> val processedResult = processJson(rawResult) completion(processedResult, exception) }) } 我很清楚如何模拟,注入,存根和validation对underlayingApi的调用。 我的问题是:代码如何看起来像validation通过completion(processedResult, exception)返回的结果completion(processedResult, exception) ?

Kotlin中的异步匿名函数? (lambdaexpression式)

我正在做一个列表查看什么时候点击调用函数。 I want to get function is async or sync 。 阻止什么时候是异步的。 甚至我想知道how attach async mark to kotlin lambda expression 。 class FunctionCaller_Content(text: List, val function: List Unit )? >? = null) /* I want both of async, sync function. */ { fun isAsnyc(order: Int): Boolean = // how to get this lambda expression{function?.get(order)} is […]

Kotlin高阶函数和单方法接口的行为?

我之前在使用RxJava和Kotlin时遇到了一些问题。 我做了一些有趣的发现,我仍然感到困惑。 RxJava中有简单的Func1接口 public interface Func1 extends Function { R call(T t); } 我试图添加一个扩展方法到一个Observable ,也是一个RxJava类。 这将收集排放到谷歌Guava ImmutableListMulitmap使用Func1来映射每个项目的关键。 fun Observable.toImmutableListMultimap(keyMapper: Func1): Observable<ImmutableListMultimap> { return this.collect({ ImmutableListMultimap.builder()},{ b, t -> b.put(keyMapper.call(t), t)}).map { it.build() } } 当我试图调用这个扩展方法时,我无法编译它,它根本不理解lambdaexpression式。 ScheduledItem.all.flatMap { it.rebuildSoftTransactions } .toImmutableListMultimap { it.id /*compile error */ } .cache() 然而,当我修改扩展方法使用函数types时,发生了最奇怪的事情。 fun Observable.toImmutableListMultimap(keyMapper: (T) -> K): Observable<ImmutableListMultimap> { […]

kotlin扩展function不知道这个东西是如何工作的

我是一个非常新的学习Kotlin。 到目前为止,一切都是可以理解的,到今天为止我已经遇到了一些威胁我的代码。 我搜索了很多,并对这段代码做了一些研究。 这是我需要了解的两个扩展函数 private fun T ?.useOrDefault(default: R, usage: T.(R) -> R) = this?.usage(default) ?:default 第二个 inline fun doubleWith(first: F, second: S, runWith: F.(S) -> Unit) { first.runWith(second) } 用法 a.useOrDefault(100) { getInteger(R.styleable.ArcSeekBar_maxProgress, it) } set(progress) { field = bound(0, progress, Int.MAX_VALUE) drawData?.let { drawData = it.copy(maxProgress = progress) } invalidate() } 我对lambda函数和高阶函数有了一个基本的理解,但是这个generics的函数实际上是作为一个初学者 提前感谢和赞赏

Kotlin拓展函数的多态性

我有几个类我不控制 ,我已经创建了几个同名的扩展方法跨几个常见的“属性”。 相同名称的扩展函数总是返回相同的值types,尽管对于每种types的接收器以不同的方式进行计算。 下面是一个基于内置types的简单例子,只有一个属性: // **DOES NOT COMPILE** // three sample classes I don’t control extended for .len inline val String.len get() = length inline val List.len get() = size inline val Sequence.len get() = count() // another class which needs to act on things with .len class Calc(val obj:T) { // HERE IS THE […]

Kotlin / Android – 带有lambda的数据类中的KotlinReflectionInternalError

kotlin.reflect.jvm.internal.KotlinReflectionInternalError:反思本地函数,lambdas,匿名函数和局部variables在Kotlinreflection中还没有完全支持 这个exception来自数据类的toString() 。 数据类包含一个lambda。 我不能在我的环境中重现它。 我是否需要重写toString()来排除lambda? 或者lambda在数据类中是不允许的? data class PersistJob( private val id: Int, private val delay: Long = 10_000L, private val maxDelay: Long = 60_000L, private val iteration: Int = 0, private val block: suspend (Int) -> Boolean) { fun getDelay() = minOf(delay, maxDelay) fun withDelayIncreased() = copy( delay = minOf(delay * 2, maxDelay), […]

Kotlin与Java:使用lambda参数调用函数

我在kotlin文件中定义了这个函数 fun loadSubmissions(projectId: Long?, completion: (List, Exception) -> Unit) { … } 并且想要像这样从Java调用它 loadSubmissions(project.getProjectId(), (submissions, e) -> { updateSubmissions(submissions); return null; }); 同 void updateSubmissions(List submissionList) { .. } 但它给了我 Error:(226, 35) error: incompatible types: List cannot be converted to List where CAP#1 is a fresh type-variable: CAP#1 extends Submission from capture of ? extends […]