首页 > 其他分享 >Kotlin Notes - 6

Kotlin Notes - 6

时间:2023-12-04 23:11:05浏览次数:30  
标签:map val Kotlin Notes value label receiver foo

  1. To access this from an outer scope (a class, extension function, or labeled function literal with receiver) you write this@label, where @label is a label on the scope this is meant to be from:

    class A { // implicit label @A
        inner class B { // implicit label @B
            fun Int.foo() { // implicit label @foo
                val a = this@A // A's this
                val b = this@B // B's this
    
                val c = this // foo()'s receiver, an Int
                val c1 = this@foo // foo()'s receiver, an Int
    
                val funLit = lambda@ fun String.() {
                    val d = this // funLit's receiver, a String
                }
    
                val funLit2 = { s: String ->
                    // foo()'s receiver, since enclosing lambda expression
                    // doesn't have any receiver
                    val d1 = this
                }
            }
        }
    }
    
  2. Destructuring declarations

    val (name, age) = person
    
    for ((a, b) in collection) { ... }
    
    for ((key, value) in map) {
       // do something with the key and the value
    }
    
    val (_, status) = getResult()
    
    map.mapValues { entry -> "${entry.value}!" }
    map.mapValues { (key, value) -> "$value!" }
    

标签:map,val,Kotlin,Notes,value,label,receiver,foo
From: https://www.cnblogs.com/otf-notes/p/17872472.html

相关文章

  • Kotlin协程系列(三)
    1.前言前面两节,我们运用了kotlin提供的简单协程去实现了一套更易用的复合协程,这些基本上是以官方协程框架为范本进行设计和实现的。虽然我们还没有直接接触kotlin官方协程框架,但对它的绝大多数功能已经了如指掌了。本节,我们来探讨一下官方协程框架的更多功能,并将其运用到实际......
  • kotlin orm kotysa笔记
    依赖implementation("org.ufoss.kotysa:kotysa-spring-jdbc:3.2.1")implementation("org.springframework.data:spring-data-jdbc")implementation("com.alibaba:druid:1.2.20")runtimeOnly("org.postgresql:postgresql")yaml配置......
  • Kotlin Notes - 5
    InKotlin,thetypesystemdistinguishesbetweenreferencesthatcanholdnull(nullablereferences)andthosethatcannot(non-nullablereferences).Forexample,aregularvariableoftypeStringcannotholdnull:vara:String="abc"//Regul......
  • Kotlin协程系列(二)
    在进行业务开发时,我们通常会基于官方的协程框架(kotlinx.coroutines)来运用Kotlin协程优化异步逻辑,不过这个框架过于庞大和复杂,如果直接接触它容易被劝退。所以,为了我们在后续的学习中游刃有余,在使用官方给出的复合协程时能够胸有成竹,我们暂且抛开它,按照它的思路实现一个轻量......
  • Kotlin协程系列(一)
    一.协程的定义最近看了一本有关kotlin协程的书籍,对协程又有了不一样的了解,所以准备写一个关于kotlin协程系列的文章。言归正传,我们在学习一个新东西的时候,如果连这个东西"是什么"都回答不了,那么自然很难进入知识获取阶段的"为什么"和"怎么办"这两个后续环节了。因此,我们......
  • Java下跌,Kotlin闯进前15,后生可畏
    近年来,Android开发由Java转Kotlin似乎成为了一种潮流。谷歌甚至曾公开表示:“Android的开发将越来越以Kotlin为先。”当前,作为移动开发中Java的劲敌,Kotlin在Tiobe流行指数中表现强劲。根据TIOBE11月发布的编程语言排行榜,Kotlin以1.15%的占比位列第15,较之10月上升3位。而在今......
  • Kotlin Notes - 4
    Ahigher-orderfunctionisafunctionthattakesfunctionsasparameters,orreturnsafunction.fun<T,R>Collection<T>.fold(initial:R,combine:(acc:R,nextElement:T)->R):R{varaccumulator:R=initialfor(elem......
  • [题解]CF1899D Yarik and Musical Notes
    思路暴力化简公式题。假定\(b_{i}^{b_j}=b_{j}^{b_{i}}\)成立,那么有:\[2^{a_i\times2^{a_j}}=2^{a_j\times2^{a_i}}\\a_i\times2^{a_j}=a_j\times2^{a_i}\\\frac{a_i}{a_j}=\frac{2^{a_i}}{2^{a_j}}\\\frac{a_i}{a_j}=2^{a_i-a_j}\]因为\(\fra......
  • Kotlin Notes - 3
    Functionparameterscanhavedefaultvalues,whichareusedwhenyouskipthecorrespondingargument.Thisreducesthenumberofoverloads:funread(b:ByteArray,off:Int=0,len:Int=b.size,){/*...*/}Ifthelastargumentafterde......
  • 运用Kotlin开发Android应用的一些技巧
    今天的这篇文章带你学习使用Kotlin开发Android应用,并对比我们传统语言Java,让你真真切切的感受到他的美和优雅。配置项目gradle文件applyplugin:'com.android.application'applyplugin:'kotlin-android'applyplugin:'kotlin-android-extensions'dependencies{clas......