Tag: 拉姆达

如何在Kotlin中省略lambda参数?

有时我不会使用所有的lambda参数,如下面的示例中的第二个参数,这是change事件中的旧值: selected.onChange { value, _ -> checkBox.isChecked = value } 我如何省略它们? 导致代码混乱,并伤害可读性。 以前我用过上面的方法,但是在kotlin 1.0.0-beta-1103下划线是保留的。

在lambda中使用return?

在下面的代码中,我想显示空的视图,如果旅行是空的,然后返回并避免运行下面的代码,但编译器说:“返回不允许在这里”。 mainRepo.fetchUpcomingTrips { trips -> if (trips.isEmpty()) { showEmptyViews() return } // run some code if it's not empty } 有没有办法像那样回报? 我知道我可以把它放在一个if else块中,但是如果有其他情况,我讨厌写作,当我有更多的条件的时候,我不太理解/可读。

函数到lambda表达式

我有一个数据类,我需要初始化一些List<String> 。 我需要得到一个JsonArray的值(我正在使用Gson)。 我做了这个功能: private fun arrayToList(data: JsonArray, key: String): List<String> { val list = mutableListOf<String>() data.forEach { a -> list.add(a[key].asString) } return list } 我如何转换为lambda表达式? 谢谢。

没有lambda的多行/复杂属性初始值设定项

我想初始化val属性,但属性初始值设定项是复杂的(包含几行代码): private val properties: Properties = { val properties = Properties() val stream = Application::class.java.getResourceAsStream("application.properties") properties.load(stream) properties }.invoke() 是否有可能不使用lambda重写此属性初始值设定项?

带Kotlin的LMAX Disruptor:不能使用lambda?

例如在Java中,我可以用LMAX disruptor做到这一点: Disruptor<NetworkEvent> disruptor = new Disruptor<>(NetworkEvent::new, 2048, Executors.newSingleThreadedExecutor()); 在Kotlin我尝试了这个等价物: val disruptor = Disruptor<NetworkEvent>({ NetworkEvent() }, 2048, Executors.newSingleThreadExecutor()) 但是我迎接这个错误:

Lambda在多个类型的对象表达式中

我提供了一个Foo类来完成一些work() : open class Foo() { fun work(x: T) { // Effects taking place here } } 而且我还提供了一个方法useJob() ,它使用了一个方法doJob() ,该方法使用一个doJob()方法的接口类型Bar 。 fun useJob(bar: Bar) interface Bar { fun doJob(x: T) } 事实证明, Foo.work()完成了Foo.work()所期望的工作。 但是,为了使用useJob()调用work() ,我需要写这样的东西: useJob(object : Foo(), Bar { fun doJob(x: T) { work(x) } }) 有没有办法使用lambda而不是这个blob? 编辑:@ jrtapsell评论让我意识到Foo实际上是开放的。

Kotlin为lambda生成内部类

在我的代码中,我有这样的东西: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) presenter.onCreate() fabContainer.onClick { presenter.onLoginButtonClicked(…) } } 当我反编译apk和检查byteCode,这样的事情出现: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.presenter.onCreate(); Sdk23ListenersKt.onClick((ProgressFloatingActionButton) _$_findCachedViewById(com.prozis.prozisgo.prozisgo.R.id.fabContainer), new ActAuthLogin$onCreate$1(this)); } final class ActAuthLogin$onCreate$1 extends Lambda implements Function1<View, Unit> { final /* synthetic */ ActAuthLogin this$0; ActAuthLogin$onCreate$1(ActAuthLogin actAuthLogin) { this.this$0 = actAuthLogin; super(1); } public final void invoke(View it) { […]

传递lambda代替接口

我创建了一个界面: interface ProgressListener { fun transferred(bytesUploaded: Long) } 但可以只用作匿名类,而不是lambda dataManager.createAndSubmitSendIt(title, message, object : ProgressListener { override fun transferred(bytesUploaded: Long) { System.out.println(bytesUploaded.toString()) } }) 我认为这应该是一个可能性,以lambda取代它: dataManager.createAndSubmitSendIt(title, message, {System.out.println(it.toString())}) 但是我得到错误:类型不匹配; 需要 – ProgressListener,找到 – () – >单位? 我究竟做错了什么?

参考Kotlin中特定实例的方法

在Java 8中,我们可以引用一个类的实例的方法。 这是一个例子 Function1<Integer, Object> ref = a::getItem; a是具有方法Object getItem(int i)的类Adapter实例。 我们可以在Kotlin做同样的事吗? 我尝试了相同的语法没有成功。 到目前为止,我只能创建一个扩展方法的引用,如下所示: val ref: Adapter.(Int) -> Any = Adapter::getItem 但是在这里我仍然需要一个Adapter的实例来调用它。 我看到的另一种替代方法是定义一个这样的lambda: val ref: (Int) -> Any = { a.getItem(it) }

如何将lambda表达式放在mapTo调用合法语法之后?

我发现了一段我不明白的代码。 我正在将JSONArray转换为List 。 Kotlin在stdlib ( 链接 )中提供了函数mapTo , mapTo inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo( destination: C, transform: (T) -> R ): C (source) 将给定的转换函数应用于原始集合的每个元素,并将结果附加到给定的目标。 这个函数有2个参数,可以像这样使用(按预期): (0..jsonArray.length() – 1).mapTo(targetList, {it -> jsonArray[it].toString()}) 但显然这也是有效的语法(不期望): (0..jsonArray.length()-1).mapTo(targetList) {it -> jsonArray[it].toString()} 正如你所看到的,函数参数在outputList之后结束,并且lambda表达式刚好放在函数调用的最后。 此外,这是合法的(如预期): val transformation = {it : Int -> jsonArray[it].toString()} (0..jsonArray.length()-1).mapTo(targetList, transformation) 但这不是(???): val transformation […]